没做过数据库的,比较菜,有个题目求教一下,
本机已经建好一个数据库 库名test 表名m_user, 内容包括name password age,内容显示在窗体上,以三个text分别显示 ,现在已经可以读取数据库内容 ,请问如何更新呢,点button按钮将text里修改过的内容传入数据库,比较菜啊 ,大家别骂我啊
//提取文本框中内容
string temp_age = this.ageTextBox.Text;
string temp_pwd = this.passwordTextBox.Text;
string temp_name = this.nameTextBox.Text; DataSet thisDataSet = new DataSet();
//构建一个主键
//DataColumn[] keys = new DataColumn[1];
//keys[0] = thisDataSet.Tables["m_user"].Columns["name"];
//thisDataSet.Tables["m_user"].PrimaryKey = keys;
thisAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
//找到一个匹配行
thisAdapter.Fill(thisDataSet,"m_user");
DataRow findRow = thisDataSet.Tables["m_user"].Rows.Find(temp_name); //对该行的值进行改变
thisDataSet.Tables["m_user"].Rows[1]["password"] = temp_pwd;
thisDataSet.Tables["m_user"].Rows[1]["age"] = temp_age; //数据库更新
thisAdapter.Update(thisDataSet, "m_user"); 最好能给出代码 ,另外再问一下给分怎么给的
本机已经建好一个数据库 库名test 表名m_user, 内容包括name password age,内容显示在窗体上,以三个text分别显示 ,现在已经可以读取数据库内容 ,请问如何更新呢,点button按钮将text里修改过的内容传入数据库,比较菜啊 ,大家别骂我啊
//提取文本框中内容
string temp_age = this.ageTextBox.Text;
string temp_pwd = this.passwordTextBox.Text;
string temp_name = this.nameTextBox.Text; DataSet thisDataSet = new DataSet();
//构建一个主键
//DataColumn[] keys = new DataColumn[1];
//keys[0] = thisDataSet.Tables["m_user"].Columns["name"];
//thisDataSet.Tables["m_user"].PrimaryKey = keys;
thisAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
//找到一个匹配行
thisAdapter.Fill(thisDataSet,"m_user");
DataRow findRow = thisDataSet.Tables["m_user"].Rows.Find(temp_name); //对该行的值进行改变
thisDataSet.Tables["m_user"].Rows[1]["password"] = temp_pwd;
thisDataSet.Tables["m_user"].Rows[1]["age"] = temp_age; //数据库更新
thisAdapter.Update(thisDataSet, "m_user"); 最好能给出代码 ,另外再问一下给分怎么给的
解决方案 »
- winform简单图片切换效果求解.
- WebService如何打包?
- C# 如何把控件 treeView1的值 copy一份给 treeView1?要值传递 急用
- wzy_love_sly,来来来。。。来下。。。
- 马上交给朝阳区政府的货,出了问题(十万火急)
- oraacle 存储过程 老是 有问题
- dropdownlist数据选择问题
- 怎么写成程序集dll
- C# 能利用未知控件的句柄(Handle)来修改该控件的鼠标(Cursor)吗?
- C#如何实现文件夹共享??高分求救!急等!!
- 字符串数组类型的object,如何转换为string[],急!急!急!100分
- 自定义控件中如何捕获鼠标双击事件???100分
thisAdapter.Fill(thisDataSet,"m_user");
試試
http://blog.csdn.net/knight94/archive/2006/04/15/664530.aspx需要为DataAdapter初始化其他Command。
{
try
{
SqlConnection conn = new SqlConnection(PubVar.PubStr);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("delete * form sms_sendout_info",cn); //注意这一句,更新数据库要初始花它的InsertCommand等4个Command
cn.Open();
DataSet ds = new DataSet();
ds.Tables.Add("sms_sendout_info");
sda.Fill(ds);
sda.Update(ds,"sms_sendout_info");
}
catch (Exception err)
{
throw new ApplicationException(err.Message); } }
//从数据库中修改指定记录
SqlConnection thisConnection = new SqlConnection(); string strUpdt = " UPDATE m_user SET password = '"
+ passwordTextBox.Text + "' , age = "
+ ageTextBox.Text + " WHERE name = " + nameTextBox.Text;
SqlCommand myCommand = new SqlCommand (strUpdt, thisConnection);
myCommand.ExecuteNonQuery();
还是报错 ExecuteNonQuery requires an open and available Connection. The connection's current state is closed. 何故了,跟踪了一下好象出现在最后一句上
差一句
thisConnection.Open();
thisConnection.Open();
string strDele = "DELETE FROM m_user WHERE name = ' " +nameTextBox.Text.Trim() + " ' ";
SqlCommand myCommand = new SqlCommand(strDele, thisConnection);
myCommand.ExecuteNonQuery();