ADO.NET基础
一、在visual studio中调用数据库有两种方式:
1、先在SQL Server Management Studio中建好数据库,然后将建好的数据库附加到visual studio中。使用这种方式不用加入额外的代码,就可以直接使用字符串连 接函数。
2、直接在visual studio中创建数据库(使用内嵌mdf文件的形式)。使用这种方式需要加入额外的代码(将代码添加到Main()函数中),代码如下:
View Code
1 stringdataDir=AppDomain.CurrentDomain.BaseD if (dataDir.EndsWith(@"\bin\Debug\") || dataDir.EndsWith(@"\bin\Release\"))
3 {
4dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
5 AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
6 }
二、创建字符串处理函数,控制数据库:
View Code1
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
三、以上内容的一个完整示例:
View Code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Data.SqlClient;
6
7 namespace ConsoleApplication1
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 string dataDir = AppDomain.CurrentDomain.BaseDirectory;
14 if (dataDir.EndsWith(@"\bin\Debug\") || dataDir.EndsWith(@"\bin\Release\"))
15 {
16 dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
17 AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
18 }
19
20 SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
21 conn.Open();
22 Console.WriteLine("打开数据库连接成功");
23 }
24 }
25 }
四、以上内容完整示例的改进版:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Data.SqlClient;
6
7 namespace ConsoleApplication1
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 string dataDir = AppDomain.CurrentDomain.BaseDirectory;
14 if (dataDir.EndsWith(@"\bin\Debug\") || dataDir.EndsWith(@"\bin\Release\"))
15 {
16 dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
17 AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
18 }
19
20 using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"))
21 {
22 conn.Open();
23 }
24 Console.WriteLine("打开数据库连接成功");
25 }
26 }
27 }
代码中将字符串处理函数部分包含进了using语句中,它的作用是自动释放using语句块中的对象的内存。
所有实现了IDisposable接口的对象,都需要使用using语句手动释放该对象的内存空间。
以上代码中,SqlConnection类的conn对象实现了IDisposable接口,所以需要使用using语句,代码中,conn出了using语句块大括号的范围便被自动释放。
五、将字面量数据插入数据库的相关表中
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Data.SqlClient;
6
7 namespace ConsoleApplication1
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 string dataDir = AppDomain.CurrentDomain.BaseDirectory;
14 if (dataDir.EndsWith(@"\bin\Debug\") || dataDir.EndsWith(@"\bin\Release\"))
15 {
16 dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
17 AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
18 }
19
20 using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"))
21 {
22 conn.Open();
23 using (SqlCommand cmd = conn.CreateCommand())
24 {
25 cmd.CommandText = "insert into Users(number,name) values('6','gan')";
26 cmd.ExecuteNonQuery();
27 Console.WriteLine("成功插入数据");
28 }
29 }
30 Console.WriteLine("打开数据库连接成功");
31 }
32 }
33 }
六、将用户输入的数据插入数据库的相关表中
View Code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Data.SqlClient;
6
7 namespace ConsoleApplication1
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 string dataDir = AppDomain.CurrentDomain.BaseDirectory;
14 if (dataDir.EndsWith(@"\bin\Debug\") || dataDir.EndsWith(@"\bin\Release\"))
15 {
16 dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
17 AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
18 }
19
20 Console.WriteLine("请输入用户号码");
21 string m_number = Console.ReadLine();
22 Console.WriteLine("请输入用户名");
23 string m_name = Console.ReadLine();
24
25 using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"))
26 {
27 conn.Open();
28 using (SqlCommand cmd = conn.CreateCommand())
29 {
30 cmd.CommandText = "insert into Users(number,name) values(' "+m_number+" ' , ' "+m_name+" ')";
31 cmd.ExecuteNonQuery();
32 Console.WriteLine("成功插入数据");
33 }
34 }
35 Console.WriteLine("打开数据库连接成功");
36 }
37 }
38 }
其中第30行代码的理解是难点(字符串连接符、向数据库中写入数据)
cmd.CommandText = "insert into Users(number,name) values(' "+m_number+" ' , ' "+m_name+" ')";
其实它是将多个字符串组成的字符串赋值给cmd.CommandText属性,多个字符串分别是:
"insert into Users(number,name) values(' "
m_number
" ' , ' "
m_name
" ')"