using (Conn = new SqlConnection(ConnectionString))
{
//DataAdapter插入数据
Conn.Open();//打开数据库连接
string sqlstr = "select * from UserInfo2";
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand insertcmd = new SqlCommand(sqlstr, Conn);
SqlCommandBuilder cb = new SqlCommandBuilder(da);//自动生成插入数据的SQL语句,不能少
da.SelectCommand = insertcmd;//必须设置此项
DataSet ds = new DataSet();
int count = da.Fill(ds, "UserInfo2");//填充DataSet
if (count > 0)
{
DataRow dr = ds.Tables["UserInfo2"].NewRow();//添加新行
dr["UserName"] = "小王";
dr["Pwd"] = "123";
ds.Tables["UserInfo2"].Rows.Add(dr);//此句不能少
da.Update(ds, "UserInfo2");//更新数据库
}
ds.Clear();
Response.Write("添加数据成功!");
}