代码:
OleDbDataAdapter oa;
DataSet ds;
private void button1_Click(object sender, EventArgs e)
{
OleDbCommand command1 = new OleDbCommand();
command1.Connection = this.con();
command1.CommandText = "select * from test";
OleDbCommand command2 = new OleDbCommand();
command2.Connection = this.con();
command2.CommandText = "update test set [name]=?,[sex]=? where [id]=?";//怀疑格式有无
OleDbParameter testname = new OleDbParameter("name",OleDbType.Char ,10,"name");
OleDbParameter testsex = new OleDbParameter("sex", OleDbType.Char , 10, "sex");
OleDbParameter testid = new OleDbParameter("id", OleDbType.Integer , 4, "id");
command2.Parameters.Add(testname );
command2.Parameters.Add(testid);
command2.Parameters.Add(testsex);
oa = new OleDbDataAdapter();
oa.SelectCommand = command1;
oa.UpdateCommand = command2;
ds = new DataSet();
oa.Fill(ds); this.dataGrid1.DataSource = ds;
this.dataGrid1.DataMember = ds.Tables[0].ToString ();
}
public OleDbConnection con()
{
OleDbConnection com = new OleDbConnection();
com.ConnectionString = "Provider=SQLNCLI.1;Data Source=.;Integrated Security=SSPI;Initial Catalog=DATABASE";
return com;
} private void button2_Click(object sender, EventArgs e)
{
DataSet dschange = new DataSet();
dschange = ds.GetChanges();
if (dschange != null)
{
try
{
oa.Update(dschange);//发现updata中的where 语句[id]取决于修改后的[sex]字段
MessageBox.Show("成功!" + "," + dschange.Tables[0].Rows[0]["id"].ToString());
button1_Click(sender, e);
}
catch(Exception er)
{
MessageBox.Show("失败!"+","+oa.UpdateCommand.Parameters ["id"].Value .ToString ()+er.Message .ToString ());
}
}
}
各位大侠帮帮忙
OleDbDataAdapter oa;
DataSet ds;
private void button1_Click(object sender, EventArgs e)
{
OleDbCommand command1 = new OleDbCommand();
command1.Connection = this.con();
command1.CommandText = "select * from test";
OleDbCommand command2 = new OleDbCommand();
command2.Connection = this.con();
command2.CommandText = "update test set [name]=?,[sex]=? where [id]=?";//怀疑格式有无
OleDbParameter testname = new OleDbParameter("name",OleDbType.Char ,10,"name");
OleDbParameter testsex = new OleDbParameter("sex", OleDbType.Char , 10, "sex");
OleDbParameter testid = new OleDbParameter("id", OleDbType.Integer , 4, "id");
command2.Parameters.Add(testname );
command2.Parameters.Add(testid);
command2.Parameters.Add(testsex);
oa = new OleDbDataAdapter();
oa.SelectCommand = command1;
oa.UpdateCommand = command2;
ds = new DataSet();
oa.Fill(ds); this.dataGrid1.DataSource = ds;
this.dataGrid1.DataMember = ds.Tables[0].ToString ();
}
public OleDbConnection con()
{
OleDbConnection com = new OleDbConnection();
com.ConnectionString = "Provider=SQLNCLI.1;Data Source=.;Integrated Security=SSPI;Initial Catalog=DATABASE";
return com;
} private void button2_Click(object sender, EventArgs e)
{
DataSet dschange = new DataSet();
dschange = ds.GetChanges();
if (dschange != null)
{
try
{
oa.Update(dschange);//发现updata中的where 语句[id]取决于修改后的[sex]字段
MessageBox.Show("成功!" + "," + dschange.Tables[0].Rows[0]["id"].ToString());
button1_Click(sender, e);
}
catch(Exception er)
{
MessageBox.Show("失败!"+","+oa.UpdateCommand.Parameters ["id"].Value .ToString ()+er.Message .ToString ());
}
}
}
各位大侠帮帮忙
解决方案 »
- 请教:ComboBox控件SelectedIndexChanged事件中修改Text属性问题
- 一般公司的会员页面是怎么设置的
- AJAX级联目录示例问题
- 关于focus的涵义
- 用卡巴斯基清除trojan.win32.agent.病毒后,打不开C盘怎么办?
- TreeView的checkbox问题,请求各位高手解决下(急!)
- 关于打印的问题
- 怎么把网址中的 %E6 之类的编码转为 中文
- 怎么开起winform监听程序,然后实现自定义时间与路径,控制服务器上自动备份文件和数据库
- 寻合作
- 执行到 oc.Parameters.Add(projectnumberparameter); 这句的时候后面的就不会执行了,直接到了catch中
- 再问正则问题(也许很简单,就是写不出)
OleDbParameter testname = new OleDbParameter("name",OleDbType.Char ,10,"name");
OleDbParameter testsex = new OleDbParameter("sex", OleDbType.Char , 10, "sex");
OleDbParameter testid = new OleDbParameter("id", OleDbType.Integer , 4, "id");SQLSERVER使用的是@,比如:
command2.CommandText = "update test set [name]=@name,[sex]=@sex where [id]=@id";MYSQL使用的是?,比如:
command2.CommandText = "update test set [name]=?name,[sex]=?sex where [id]=?id"在使用access数据时
如果 CommandType 设置为 Text,OLE DB.NET 提供程序不支持将参数传递给 OleDbCommand 调用的 SQL 语句或存储过程的命名参数。在这种情况下,必须使用问号 (?) 占位符。例如: command2.CommandText = "update test set [name]=?,[sex]=? where [id]=?";注意,后面的你要添加参数时,OleDbParameterCollection 的顺序必须直接对应于参数的问号占位符的位置。