自己写了一个简单的数据库操作类、封装了一些简单的操作、

  1 using System;
2 using System.Data.SqlClient;
3 using System.Data;
4
5 namespace DemoConsoleApplication
6 {
7 class SQLHelper
8 {
9 private static string FLastError;
10 /// <summary>
11 /// 最近一次异常信息
12 /// </summary>
13 public static string LastError
14 {
15 get { return FLastError; }
16 }
17
18 /// <summary>
19 /// 通用异常处理函数
20 /// </summary>
21 /// <param name="e">需要处理的异常</param>
22 private static void HandleException(Exception e)
23 {
24 if (e is SqlException)
25 {
26 FLastError = string.Format("在打开连接时出现连接级别的错误:{0}", e.Message);
27 }
28 else if (e is InvalidOperationException)
29 {
30 FLastError = e.Message;
31 }
32 else if (e is DBConcurrencyException)
33 {
34 FLastError = string.Format("尝试执行 INSERT、UPDATE 或 DELETE 语句,但没有记录受到影响:{0}", e.Message);
35 }
36 else
37 {
38 FLastError = string.Format("未预料的异常:", e.Message);
39 }
40 }
41 /// <summary>
42 /// 获取数据库连接字符串、子类覆盖后自行修改
43 /// </summary>
44 /// <returns>连接字符串</returns>
45 private static string GetConnectionString()
46 {
47 return null;
48 }
49
50 private static SqlConnection OpenConnection()
51 {
52 return null;
53 }
54 /// <summary>
55 /// 无返回值的SQL语句执行
56 /// </summary>
57 /// <param name="ASql">欲执行的SQL语句</param>
58 /// <param name="AParams">参数集合</param>
59 /// <returns>影响记录的行数</returns>
60 public static int ExecNonSQL(string ASql, params SqlParameter[] AParams)
61 {
62 using (SqlConnection Conn = new SqlConnection(GetConnectionString()))
63 {
64 try
65 {
66 //Open异常捕获
67 Conn.Open();
68 using (SqlCommand Cmd = Conn.CreateCommand())
69 {
70 Cmd.CommandText = ASql;
71 foreach (SqlParameter param in AParams)
72 {
73 Cmd.Parameters.Add(param);
74 }
75 return Cmd.ExecuteNonQuery();
76 }
77 }
78 catch (Exception ex)
79 {
80 HandleException(ex);
81 }
82 }
83
84 return -1;
85 }
86
87 /// <summary>
88 /// 获得离线数据集合
89 /// </summary>
90 /// <param name="ASql">欲执行的SQL语句</param>
91 /// <param name="AParams">与SQL相关的参数</param>
92 /// <returns>返回查询结果集合</returns>
93 public static DataSet ExecSQLByDataSet(string ASql, params SqlParameter[] AParams)
94 {
95 using (SqlConnection Conn = new SqlConnection(GetConnectionString()))
96 {
97 try
98 {
99 Conn.Open();
100 using (SqlCommand Cmd = Conn.CreateCommand())
101 {
102 Cmd.CommandText = ASql;
103 foreach (SqlParameter param in AParams)
104 {
105 Cmd.Parameters.Add(param);
106 }
107 SqlDataAdapter Adapter = new SqlDataAdapter(Cmd);
108 DataSet Result = new DataSet();
109 Adapter.Fill(Result);
110 return Result;
111 }
112 }
113 catch (Exception ex)
114 {
115 HandleException(ex);
116 }
117 }
118 return null;
119 }
120
121 /// <summary>
122 /// 将本地修改的结果集提交至服务器
123 /// </summary>
124 /// <param name="ADataSet">已修改的结果集合</param>
125 public static void UpdateByDataSet(DataSet ADataSet)
126 {
127 using (SqlConnection Conn = new SqlConnection(GetConnectionString()))
128 {
129 Conn.Open();
130 using (SqlCommand Cmd = Conn.CreateCommand())
131 {
132 SqlDataAdapter Adapter = new SqlDataAdapter(Cmd);
133 new SqlCommandBuilder(Adapter);
134 Adapter.ContinueUpdateOnError = true;
135 try
136 {
137 Adapter.Update(ADataSet);
138 }
139 catch (Exception ex)
140 {
141 HandleException(ex);
142 }
143 }
144 }
145 }
146 }
147 }

  简单的使用方法演示:

 1 using System;
2 using System.Data.SqlClient;
3
4 namespace DemoConsoleApplication
5 {
6 class Program
7 {
8 //数据库连接字符串、根据实际修改
9 private const string ConnectionString = @"Data Source=|DataDirectory|\Database1.sdf";
10
11 static void Main(string[] args)
12 {
13 //获取用户输入的内容
14 Console.WriteLine("请输入用户名");
15 string Passport = Console.ReadLine();
16 Console.WriteLine("请输入密码");
17 string Password = Console.ReadLine();
18
19 if (1 == SQLHelper.ExecNonSQL("select * from TB_Users where passport=@UN and password=@PWD",
20 new SqlParameter("UN", Passport),new SqlParameter("PWD", Password)))
21 Console.WriteLine("登陆成功!");
22 else
23 Console.WriteLine("登陆失败!");
24 Console.ReadKey();//防止控制台程序一闪而过、而看不到输出结果
25 }
26 }
27 }