windows窗体中有多个textbox,他们的text属性都绑定了某一表的某一列~~~~~~~~~~~~~~~~~~~~~~~~~~~~说不明白。直接代码吧!!!!
数据绑定:
this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text",this.cyxjBindingSource, "cyzd1", true));
this.textBox2.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.cyxjBindingSource, "cyzd", true));
this.textBox3.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.cyxjBindingSource, "cyyz", true));
数据更新:
if(sRead.Read() == false)\\\\插入
{
DataRow row = this.dianZiBingLiDataSet8.cyxj.NewRow();
row["cyzd1"] = richTextBox1.Text;
row["cyzd"] = richTextBox2.Text;
row["cyyz"] = richTextBox3.Text;
this.dianZiBingLiDataSet8.cyxj.Rows.Add(row);
}
else\\\更新
{
foreach (DataRow row in this.dianZiBingLiDataSet8.cyxj.Rows)
{
row.BeginEdit();
row["cyzd1"] = richTextBox1.Text;
row["cyzd"] = richTextBox2.Text;
row["cyyz"] = richTextBox3.Text;
row.EndEdit();
}
try
{
this.cyxjTableAdapter.Update(this.dianZiBingLiDataSet8.cyxj);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}这样做是能更新和插入数据的,但它存在大量的赋值,就太麻烦了!!!有没有这种方法,我不用赋值,就直接把控件中的更改用update更新或插入到数据库中呢!!!! 刚刚开始学习,请大家帮忙,给点意见。
数据绑定:
this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text",this.cyxjBindingSource, "cyzd1", true));
this.textBox2.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.cyxjBindingSource, "cyzd", true));
this.textBox3.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.cyxjBindingSource, "cyyz", true));
数据更新:
if(sRead.Read() == false)\\\\插入
{
DataRow row = this.dianZiBingLiDataSet8.cyxj.NewRow();
row["cyzd1"] = richTextBox1.Text;
row["cyzd"] = richTextBox2.Text;
row["cyyz"] = richTextBox3.Text;
this.dianZiBingLiDataSet8.cyxj.Rows.Add(row);
}
else\\\更新
{
foreach (DataRow row in this.dianZiBingLiDataSet8.cyxj.Rows)
{
row.BeginEdit();
row["cyzd1"] = richTextBox1.Text;
row["cyzd"] = richTextBox2.Text;
row["cyyz"] = richTextBox3.Text;
row.EndEdit();
}
try
{
this.cyxjTableAdapter.Update(this.dianZiBingLiDataSet8.cyxj);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}这样做是能更新和插入数据的,但它存在大量的赋值,就太麻烦了!!!有没有这种方法,我不用赋值,就直接把控件中的更改用update更新或插入到数据库中呢!!!! 刚刚开始学习,请大家帮忙,给点意见。
绑定可以这样: txtStudentNo.DataBindings.Add("Text", sqlDs, "学生信息.学号");
txtName.DataBindings.Add("Text", sqlDs, "学生信息.姓名");
txtShenFen.DataBindings.Add("Text", sqlDs, "学生信息.身份证号");数据用SqlDataAdapter查询后,填充到sqlDs(一个DataSet对象中),然后判断是否有修改:
if ((sqlDs.HasChanges == true)) {
sqlDs.Tables(0).Rows(bmData.Position).EndEdit();
//停止当前的任何编辑。
if (isAdd == true) {
Text = "学生信息-继续新增";
btnAdd.Enabled = true;
}
try {
if (sqlDs.HasChanges == true) {
sqlDa.Update(sqlDs, "学生信息");
sqlDs.AcceptChanges();
MessageBox.Show("更新记录成功!", "提示信息");
}
} catch (Exception ex) {
MessageBox.Show(ex.ToString);
}
} else {
MessageBox.Show("你还未做任何修改!");
}
sqlDs是否有修改:if (sqlDs.HasChanges() == true) {
sqlDa.Update(sqlDs, "学生信息");
sqlDs.AcceptChanges();
MessageBox.Show("更新记录成功!", "提示信息");
}这里调用sqlDa.Update,就可以更新,前提是sqlDa已经设置好了InsertCommand(用于实现新增记录)、DeleteCommand(用于实现删除记录)、UpdateCommand(用于修改记录)
可以通过下面的一行来搞定:SqlCommandBuilder builder=new SqlCommandBuilder(sqlDa);//这样就自动为sqlDa生成了上述命令
也可以自定义:private void BuildAdapterCommands(string strSelectSQL)
{
//设置数据适配器的SelectCommand属性
sqlDa.SelectCommand = new SqlCommand(strSelectSQL, conn); //定义用来新增记录的Sql语句,使用参数
string strInsertComm = "Insert Into 学生信息(学号, 姓名, 性别, 身份证号, 班级编号, 籍贯编号,学籍编号, 政治面貌编号, 民族编号)" + " Values(@Number,@Name,@Sex,@PId,@ClassId,@BirthPlaceId,@StatusId,@PolityId,@NationId )"; //设置数据适配器的InsertCommand属性
sqlDa.InsertCommand = new SqlCommand(strInsertComm, conn); //设置InsertCommand中的各个参数
sqlDa.InsertCommand.Parameters.Add("@Number", SqlDbType.NVarChar, 12, "学号");
sqlDa.InsertCommand.Parameters.Add("@Name", SqlDbType.NVarChar, 12, "姓名");
sqlDa.InsertCommand.Parameters.Add("@Sex", SqlDbType.NVarChar, 2, "性别");
sqlDa.InsertCommand.Parameters.Add("@PId", SqlDbType.NVarChar, 18, "身份证号");
sqlDa.InsertCommand.Parameters.Add("@ClassId", SqlDbType.NVarChar, 10, "班级编号");
sqlDa.InsertCommand.Parameters.Add("@BirthPlaceId", SqlDbType.NVarChar, 2, "籍贯编号");
sqlDa.InsertCommand.Parameters.Add("@StatusId", SqlDbType.NVarChar, 2, "学籍编号");
sqlDa.InsertCommand.Parameters.Add("@PolityId", SqlDbType.NVarChar, 2, "政治面貌编号");
sqlDa.InsertCommand.Parameters.Add("@NationId", SqlDbType.NVarChar, 2, "民族编号"); //定义用来修改数据的命令字符串
string strUpdateComm = "Update 学生信息 Set 学号=@Number,姓名=@Name,性别=@Sex,身份证号=@PId," + "班级编号=@ClassId,籍贯编号=@BirthPlaceId," + "学籍编号=@StatusId,政治面貌编号=@PolityId," + "民族编号=@NationId Where 学号=@Number"; //设置数据适配器的UpdateCommand属性
sqlDa.UpdateCommand = new SqlCommand(strUpdateComm, conn); //设置Update语句中的各个参数
sqlDa.UpdateCommand.Parameters.Add("@Name", SqlDbType.NVarChar, 12, "姓名");
sqlDa.UpdateCommand.Parameters.Add("@Sex", SqlDbType.NVarChar, 2, "性别");
sqlDa.UpdateCommand.Parameters.Add("@PId", SqlDbType.NVarChar, 18, "身份证号");
sqlDa.UpdateCommand.Parameters.Add("@ClassId", SqlDbType.NVarChar, 10, "班级编号");
sqlDa.UpdateCommand.Parameters.Add("@BirthPlaceId", SqlDbType.NVarChar, 2, "籍贯编号");
sqlDa.UpdateCommand.Parameters.Add("@StatusId", SqlDbType.NVarChar, 2, "学籍编号");
sqlDa.UpdateCommand.Parameters.Add("@PolityId", SqlDbType.NVarChar, 2, "政治面貌编号");
sqlDa.UpdateCommand.Parameters.Add("@NationId", SqlDbType.NVarChar, 2, "民族编号"); //避免学号被修改后,更新失败,必须制定学号取原始值!
SqlParameter KeywordParaUpdate = sqlDa.UpdateCommand.Parameters.Add("@Number", SqlDbType.NVarChar, 12, "学号");
KeywordParaUpdate.SourceVersion = DataRowVersion.Original; //*******************************
//定义用来删除数据的命令字符串
string strDeleteComm = "Delete 学生信息 Where 学号=@Number"; //设置数据适配器的DeleteCommand属性
sqlDa.DeleteCommand = new SqlCommand(strDeleteComm, conn);
//设置Delete语句中的各个参数 //一样要指定用学号的原始值
SqlParameter KeywordParaDelete = sqlDa.DeleteCommand.Parameters.Add("@Number", SqlDbType.NVarChar, 12, "学号");
KeywordParaDelete.SourceVersion = DataRowVersion.Original;
}
还可使用sqlbuilder更新数据
SqlCommandBuilder myBuilder = new SqlCommandBuilder(sqlda);
sqlda.Update(myds);
string conStr = "server=SERVER.;database=DianZiBingLi;uid=sa;password=wzh1103";
SqlConnection conn = new SqlConnection(conStr);
string sqlStr = "select * from [ryqk] where bah ='" + textBox14.Text.Trim() + "'";
SqlDataAdapter ada = new SqlDataAdapter(conStr, conn);
ada.Fill(this.dianZiBingLiDataSet8,"ryqk");
运行到ada.Fill(this.dianZiBingLiDataSet8,"ryqk");报第1行:'='附近有语法错误.
请帮忙看看!!!
但有个问题搞不明白,请指教:
保存按钮:
try
{
if (this.dianZiBingLiDataSet8.ryqk.DataSet.HasChanges()== true)
{
this.ryqkTableAdapter.Update(this.dianZiBingLiDataSet8.ryqk);
// sqlDs.Update(sqlDs, "ryqk"); this.dianZiBingLiDataSet8.ryqk.AcceptChanges();
MessageBox.Show("更新记录成功!", "提示信息");
} else
{
MessageBox.Show("111111");
}数据填充:
string conStr = "server=SERVER.;database=DianZiBingLi;uid=sa;password=wzh1103";
SqlConnection conn = new SqlConnection(conStr);
string sqlStr = "select * from [ryqk] where bah ='" + textBox14.Text.Trim() + "'";
SqlDataAdapter ada = new SqlDataAdapter(sqlStr, conn);
ada.Fill(this.dianZiBingLiDataSet8, "ryqk");
}现在问题是,this.dianZiBingLiDataSet8.ryqk.DataSet.HasChanges()== true 不能检测控件的更新,它不起用。
我应该怎么改。
我应该怎么改。先停止当前的编辑处理:this.dianZiBingLiDataSet8.Tables["ryqk"].CurrentRow.EndEdit();//停止当前的任何编辑。
选项卡的单击事件:
private void tabControl2_Click(object sender, EventArgs e)
{
if (tabControl2.SelectedTab != tabPage1)
{
textBox14.Text = textBox94.Text=textBox4.Text;
textBox37.Text = textBox95.Text=textBox5.Text;
string conStr = "server=SERVER.;database=DianZiBingLi;uid=sa;password=wzh1103";
SqlConnection conn = new SqlConnection(conStr);
string sqlStr = "select * from [ryqk] where bah ='" + textBox14.Text.Trim() + "'";
SqlDataAdapter ada = new SqlDataAdapter(sqlStr, conn);
ada.Fill(this.dianZiBingLiDataSet8, "ryqk");
}
else
{
tabControl1.Enabled = true;
}
保存按钮:
else if (tabControl2.SelectedTab == tabPage3)
{
this.dianZiBingLiDataSet8.ryqk.EndInit();
if(this.dianZiBingLiDataSet8.ryqk.DataSet.HasChanges()==true)
{
this.ryqkTableAdapter.Update(this.dianZiBingLiDataSet8.ryqk);
this.dianZiBingLiDataSet8.ryqk.AcceptChanges();
MessageBox.Show("更新记录成功!");
}
}
this.dianZiBingLiDataSet8.Tables["ryqk"].CurrentRow.EndEdit();\\我的代码里没有CurrentRow的定义。