最简单基本快捷连接数据库语句:conn.Open();SqlCommand cmd = new SqlCommand(sql, conn);cmd.ExecuteNonQuery();conn.Close();
缺点: 输入 ' 字符会出错。
问题解决--使用参数转递 就能解决 ' 字符输出出错问题。
需要使用sqlhelper.cs 微软推出的 方便数据库操作类
string tx = TextBox1.Text; StringBuilder strSQL = new StringBuilder(); SqlCommand cmd = new SqlCommand();
SqlParameter parm = new SqlParameter("@a2",SqlDbType.Text);
parm.Value = tx;
cmd.Parameters.Add(parm);
using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction)) { strSQL.Append(SQL_INSERT_A2);
conn.Open();
cmd.Connection = conn; cmd.CommandType = CommandType.Text; cmd.CommandText = strSQL.ToString();
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();