111,098
社区成员




SqlCommand cmd = new SqlCommand("insert into table1 values('"+mtb_name.Text+"','"+mtb_sex.Text+"')",conn);string sql = "你的insert语句";//这个不会不会写吧?-_-!
SqlCommand cmd = new SqlCommand(sql,con);
cmd.ExecuteNonQuery();
conn.Close();
//构造SQ语句:insert into
//TableName:这里要改成你的数据库中的表名
//name,sex,age:这里要改成性你数据库中相应表中的相应字段名称,如果还有其它字段且必填的你也必须列出来。
//后面是格式化,分别用文本框txtName,txtSex,txtAge中的内容填充
string sql = string.Format("insert into TableName (name,sex,age) values('{0}','{1}','{2}')",txtName.Text,txtSex.Text,txtAge.Text);
//执行这个命令
SqlCommand cmd = new SqlCommand(sql,conn);
int k = cmd.ExecuteNonQuery();
//根据返回值k判断影响的记录数,应当为1。
if(k>0)
{
//插入成功
}
string sql = string.Format("insert into TableName (name,sex,age) values('{0}','{1}','{2}')",txtName.Text,txtSex.Text,txtAge.Text);
SqlCommand cmd = new SqlCommand(sql,conn);
int k = cmd.ExecuteNonQuery();
if(k>0)
{
//插入成功
}