把数据添加到sql中
string sql = "INSERT INTO tbStudent VALUES (@Name,@Ware,@Model,@Number,@Sale,@Opinion,@Bonus)";
//string sql = "INSERT INTO tbStudent VALUES(@sNo,@sName,@sSex,@sAge)";
// 产生命令对象
SqlDataAdapter sqlAdap = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand(sql, sqlcon);
sqlAdap.InsertCommand = cmd;
// 设置参数
SqlParameter pa = cmd.Parameters.Add("@Name", SqlDbType.VarChar, 20);
pa.SourceColumn = "Name";
pa.SourceVersion = DataRowVersion.Current;
pa = cmd.Parameters.Add("@Ware", SqlDbType.VarChar, 20);
pa.SourceColumn = "Ware";
pa.SourceVersion = DataRowVersion.Current;
pa = cmd.Parameters.Add("@Model", SqlDbType.VarChar, 20);
pa.SourceColumn = "Model";
pa.SourceVersion = DataRowVersion.Current;
pa = cmd.Parameters.Add("@Number", SqlDbType.Int, 4);
pa.SourceColumn = "Number";
pa.SourceVersion = DataRowVersion.Current;
pa = cmd.Parameters.Add("@Sale", SqlDbType.Money, 20);
pa.SourceColumn = "Sale";
pa.SourceVersion = DataRowVersion.Current;
pa = cmd.Parameters.Add("@Opinion", SqlDbType.VarChar, 20);
pa.SourceColumn = "Opinion";
pa.SourceVersion = DataRowVersion.Current;
pa = cmd.Parameters.Add("@Bonus", SqlDbType.Float, 8);
pa.SourceColumn = "Bonus";
pa.SourceVersion = DataRowVersion.Current;
try
{
sqlAdap.Update(ds.Tables[0]);
ds.Tables[0].AcceptChanges();
MessageBox.Show("添加成功!");
}
catch (Exception ex)
{
MessageBox.Show("添加失败!错误描述为:" + ex.Message);
}
}