ADO.NET教程(四)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data;//必须引入 using System.Data.SqlClient;//必须引入 namespace Command { class Program { static void Main(string[] args) { string connSQL = @"Data Source=.\SQLEXPRESS; Initial Catalog=db_MyDemo; Integrated Security=SSPI";//构造连接字符串 SqlConnectionStringBuilder connStr = new SqlConnectionStringBuilder(connSQL); using(SqlConnection conn = new SqlConnection(connStr.ConnectionString)) { //拼接SQL语句 StringBuilder strSQL = new StringBuilder(); strSQL.Append("insert into tb_SelCustomer "); strSQL.Append("values("); strSQL.Append("'liuhao','0','0','13822223333','liuhaorain@163.com','广东省深圳市宝安区',12.234556,34.222234,'422900','备注信息')"); Console.WriteLine("Output SQL:\n{0}",strSQL.ToString()); //创建Command对象 SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.Text; cmd.CommandText = strSQL.ToString(); try { conn.Open();//一定要注意打开连接 int rows = cmd.ExecuteNonQuery();//执行命令 Console.WriteLine("\nResult: {0}行受影响",rows); } catch(Exception ex) { Console.WriteLine("\nError:\n{0}", ex.Message); } } Console.Read(); } } }