C#中关于数据库中插入数据的
private SqlCommand GetInsertCommand(){
string str = "server=.;database=student;uid=sa;pwd=;";
SqlConnection myconn = new SqlConnection(str);
SqlCommand mycomm = new SqlCommand("InsertStudent", myconn);
mycomm.CommandType = CommandType.StoredProcedure;
SqlParameterCollection sqlpara = mycomm.Parameters;
sqlpara.Add("@sno", SqlDbType.Int);
sqlpara.Add("@sname", SqlDbType.VarChar);
sqlpara.Add("@s易做图", SqlDbType.Bit);
sqlpara["@sno"].SourceColumn = "sno";
sqlpara["@sname"].SourceColumn = "sname";
sqlpara["@s易做图"].SourceColumn = "s易做图";
return mycomm;
}
protected void Button1_Click(object sender, EventArgs e)
{
DataSet myset = new DataSet();
DataTable mytable = new DataTable();
mytable.Columns.Add("sno", typeof(Int32));
mytable.Columns.Add("sname", typeof(String));
mytable.Columns.Add("s易做图", typeof(Boolean));
DataRow myrow = mytable.NewRow();
myrow[0] = Convert.ToInt32(NoBox.Text);
myrow[1] = NameBox.Text;
if (RadioButton1.Checked)
myrow[2] = 1;
else myrow[2] = 0;
mytable.Rows.Add(myrow);
myset.Tables.Add(mytable);
SqlDataAdapter myadapter = new SqlDataAdapter();
myadapter.InsertCommand = GetInsertCommand();
try
{
myadapter.Update(myset,"student");
Label4.Visible = true;
Label4.Text = "数据添加成功";
}
catch (Exception ee)
{
Response.Write(ee.Message);
Label4.Visible = true;
Label4.Text = "插入失败";
}
}
}
Update 无法找到 TableMapping['student'] 或 DataTable“student”。怎么回事啊?谢谢
--------------------编程问答-------------------- DataTable mytable = new DataTable();
改为
DataTable mytable = new DataTable("student");
--------------------编程问答-------------------- 看完回帖是一种美德 --------------------编程问答-------------------- 名字要一样
--------------------编程问答--------------------
应该就是这个问题 --------------------编程问答-------------------- DataTable mytable = new DataTable("student");
myadapter.Update(mytable );
--------------------编程问答-------------------- 听说回帖有5毛 --------------------编程问答--------------------
up
补充:.NET技术 , ASP.NET