日期:2014-05-17 浏览次数:20622 次
string sql = string.Format("insert into 部门表 values({0},'{1}',{2})",1,'张三',18);
string conStr=@"Data Source=ONE-TEACHERS\MSSQLSERVER2008;Initial Catalog=数据库;Integrated Security=True";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = conStr;
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
//新手一个。麻烦大家解释一下上面的程序。帮我一句一句的分析。。。。。。谢谢!
//定义字符串变量sql. 赋值要执行的TSQL语句.
string sql = string.Format("insert into 部门表 values({0},'{1}',{2})",1,'张三',18);
//定义字符串变量conStr. 赋值连接字符串.
string conStr=@"Data Source=ONE-TEACHERS\MSSQLSERVER2008;Initial Catalog=数据库;Integrated Security=True";
//创建SqlConnection对象conn,用于连接数据库.
SqlConnection conn = new SqlConnection();
//赋值conStr给conn对象连接字符串属性.
conn.ConnectionString = conStr;
//conn对象打开连接.
conn.Open();
//创建SqlCommand对象cmd.
SqlCommand cmd = new SqlCommand();
//指定cmd对象连接属性为conn.
cmd.Connection = conn;
//指定cmd对象CommandText属性为sql.
cmd.CommandText = sql;
//cmd执行sql语句.
cmd.ExecuteNonQuery();