1
1.SqlConnection对象创建的几种写法:
2
第一种:
3
string connstring = "server=localhost;user id = sa;password=;database=Northwind";
4
SqlConnection mySqlConnection = new SqlConnection();
5
mySqlConnection.ConnectionString =connstring;
6
第二种;
7
SqlConnection myconnection = new SqlConnection("server=localhost;user id =sa;password=;database=Northwind");
8
第三种:
9
string connstring = "server=localhost;user id = sa;password=;database=Northwind";
10
SqlConnection mySqlConnection = new SqlConnection(connstring);
11
12
--------------------------------------------------------------------------------
13
14
2.SqlCommand对象创建的几种写法:
15
第一种:
16
SqlConnection myconnection = new SqlConnection();
17
myconnection.ConnectionString="server=localhost;user id = sa;password=;database=Northwind";
// 创建并返回一个与SqlConnection关联的SqlCommand对象
18
SqlCommand mycommand =myconnection.CreateCommand();
19
mycommand.CommandText="Delete From Customers where CustomerID in
";//句号后为省略部份.
20
mycommand.ExecuteNonQuery();
21
第二种:
22
SqlConnection myconnection = new SqlConnection();
23
myconnection.ConnectionString ="server=localhost;user id = sa;password=;database=Northwind";
24
SqlCommand mycommand = new SqlCommand("Delete From Customers where CustomerID in
",myconnection);
25
mycommand.ExecuteNonQuery();
26
第三种:
27
SqlConnection myconnection = new SqlConnection();
28
myconnection.ConnectionString ="server=localhost;user id = sa;password=;database=Northwind";
29
string strsql = "Delete From Customers where CustomerID in
";
30
SqlCommand mycommand = new SqlCommand(strsql,myconnection);
31
mycommand.ExecuteNonQuery();
32
33
--------------------------------------------------------------------------------
34
35
3.SqlDataAdapter对象创建的几种写法:
36
第一种: 文本形式(微软推荐)
37
SqlConnection myconnection = new SqlConnection();
38
myconnection.ConnectionString ="server=localhost;user id = sa;password=;database=Northwind";
39
SqlDataAdapter mycommand = new SqlDataAdapter("
40
Delete From Customers where CustomerID in
","server=localhost;user id = sa;password=;database=Northwind");
41
42
每二种: 以参数形式(SqlCommand)
43
SqlConnection myconnection = new SqlConnection();
44
myconnection.ConnectionString="server=localhost;user id = sa;password=;database=Northwind";
45
SqlCommand mycommand = new SqlCommand("Delete From Customers where CustomerID in
",myconnection);
46
SqlDataAdapter mycommand = new SqlDataAdapter(mycommand);
47
48
第三种:
49
SqlConnection myconnection = new SqlConnection();
50
myconnection.ConnectionString="server=localhost;user id = sa;password=;database=Northwind";
51
SqlDataAdapter mycommand = new SqlDataAdapter("Delete From Customers where CustomerID in
",myconnection);
52
53
第四种:
54
SqlConnection myconnection = new SqlConnection();
55
myconnection.ConnectionString ="server=localhost;user id = sa;password=;database=Northwind";
56
string strsql="Delete From Customers where CustomerID in
";
57
SqlDataAdapter mycommand = new SqlDataAdapter(strsql,myconnection);
58
59
--------------------------------------------------------------------------------
60
61
4.打开与关闭SqlConnection的几种写法:
62
第一种:
63
SqlConnection myconnection = new SqlConnection();
64
myconnection.ConnectionString ="server=localhost;user id = sa;password=;database=Northwind";
65
SqlCommand mycommand = new SqlCommand("Delete From Customers where CustomerID in
",myconnection);
66
mycommand.Connection.open();
67
SqlDataReader myreader = mycommand.ExecuteReader();
68
mycommand.Connection.close();
69
70
第二种:
71
SqlConnection myconnection = new SqlConnection();
72
myconnection.ConnectionString ="server=localhost;user id = sa;password=;database=Northwind";
73
SqlCommand mycommand = new SqlCommand("Delete From Customers where CustomerID in
",myconnection);
74
myconnection.open();
75
SqlDataReader myreader = mycommand.ExecuteReader();
76
myconnection.close();
77
78
第三种:
79
SqlConnection myconnection = new SqlConnection();
80
myconnection.ConnectionString ="server=localhost;user id = sa;password=;database=Northwind";
81
SqlCommand mycommand = new SqlCommand("Delete From Customers where CustomerID in
",myconnection);
82
mycommand.Connection.open();
83
SqlDataReader myreader = mycommand.ExecuteReader(CommandBehavior.CloseConnection);
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 创建并返回一个与SqlConnection关联的SqlCommand对象
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
浙公网安备 33010602011771号