C# 更新MS Access c中的多行#
我试图更新MS Access数据库中的多行,但每次更新语法出现错误时,我的代码如下C# 更新MS Access c中的多行#,c#,database,syntax,oledb,ms-access-2013,C#,Database,Syntax,Oledb,Ms Access 2013,我试图更新MS Access数据库中的多行,但每次更新语法出现错误时,我的代码如下 cn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source =C:\\Users\\Kalpesh\\Desktop\\Entry.accdb"); sqlstr = "Update Members SET Name=@Name, MobileNo=@MobileNo, Password=@Password, IDType=@ID
cn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source =C:\\Users\\Kalpesh\\Desktop\\Entry.accdb");
sqlstr = "Update Members SET Name=@Name, MobileNo=@MobileNo, Password=@Password, IDType=@IDType, IDNo=@IDNo, AmountPaid=@AmountPaid, Address=@Address, Reg Date = "+DateTime.Now.ToShortDateString()+" WHERE UserName = '" + comboBox4.SelectedItem.ToString() + "'",cn);
cmd = new OleDbCommand(sqlstr, cn);
cmd.Parameters.AddWithValue("@Name", textBox19.Text.ToString());
cmd.Parameters.AddWithValue("@MobileNo", textBox14.Text.ToString());
cmd.Parameters.AddWithValue("@Password", textBox17.Text.ToString());
cmd.Parameters.AddWithValue("@IDType", comboBox5.SelectedItem.ToString());
cmd.Parameters.AddWithValue("@IDNo", textBox11.Text.ToString());
cmd.Parameters.AddWithValue("@AmountPaid", textBox16.Text.ToString());
cmd.Parameters.AddWithValue("@Address", richTextBox3.Text.ToString());
cn.Open();
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
MessageBox.Show("Record Updated");
cn.Close();
名称
和密码
在MS Access中
如果保留字已在使用中,您可以通过
用括号([]
)围绕单词的每个出现处但是,
最好的解决方案是将名称更改为不可回复的单词
您还需要使用带有方括号的Reg Date
,如[Reg Date]
,因为它有空格。将Reg Date
和UserName
列值参数化,就像对其他列所做的那样。喜欢
... [Reg Date] = @reg WHERE UserName = @user ...
cmd.Parameters.AddWithValue("@reg", DateTime.Now.ToShortDateString());
cmd.Parameters.AddWithValue("@user", comboBox4.SelectedItem.ToString());
还可用于处理OleDbConnection
和OleDbCommand
string conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =C:\\Users\\Kalpesh\\Desktop\\Entry.accdb";
using(OleDbConnection cn = new OleDbConnection(conString))
using(OleDbCommand cmd = cn.CreateCommand())
{
// Do your work..
}
名称
和密码
在MS Access中
如果保留字已在使用中,您可以通过
用括号([]
)围绕单词的每个出现处但是,
最好的解决方案是将名称更改为不可回复的单词
您还需要使用带有方括号的Reg Date
,如[Reg Date]
,因为它有空格。将Reg Date
和UserName
列值参数化,就像对其他列所做的那样。喜欢
... [Reg Date] = @reg WHERE UserName = @user ...
cmd.Parameters.AddWithValue("@reg", DateTime.Now.ToShortDateString());
cmd.Parameters.AddWithValue("@user", comboBox4.SelectedItem.ToString());
还可用于处理OleDbConnection
和OleDbCommand
string conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =C:\\Users\\Kalpesh\\Desktop\\Entry.accdb";
using(OleDbConnection cn = new OleDbConnection(conString))
using(OleDbCommand cmd = cn.CreateCommand())
{
// Do your work..
}
由于没有关于语法问题的明确错误消息,我猜是日期前后缺少单引号,请尝试以下方法:
RegDate = '"+DateTime.Now.ToShortDateString()+"'
由于没有关于语法问题的明确错误消息,我猜是日期前后缺少单引号,请尝试以下方法:
RegDate = '"+DateTime.Now.ToShortDateString()+"'
问题就在这里
"Reg Date = "+DateTime.Now.ToShortDateString()+"
试试这个
"[Reg Date] = #" + DateTime.Now.ToShortDateString() + "# "
问题就在这里
"Reg Date = "+DateTime.Now.ToShortDateString()+"
试试这个
"[Reg Date] = #" + DateTime.Now.ToShortDateString() + "# "
地狱是的,你是对的,为datetime工作添加[]fro name&password&single quot地狱是的,你是对的,为datetime工作添加[]fro name&password&single quot我不是访问方面的专家,但
Reg Date
语法看起来有误。首先,标识符中有一个空格。其次,您可能缺少日期文字周围的一些标点符号。更新工作代码sqlstr=“updatemembers SET[Name]=@Name,MobileNo=@MobileNo,[Password]=@Password,IDType=@IDType,IDNo=@IDNo,AmountPaid=@AmountPaid,Address=@Address,[Reg date]=”“+DateTime.Now.ToShortDateString()+“'WHERE UserName=””+comboBox4.选择EdItem.ToString()+“'”;我不是Access方面的专家,但Reg Date
语法似乎有误。首先,标识符中有一个空格。其次,您可能缺少日期文字周围的一些标点符号。更新工作代码sqlstr=“updatemembers SET[Name]=@Name,MobileNo=@MobileNo,[Password]=@Password,IDType=@IDType,IDNo=@IDNo,AmountPaid=@AmountPaid,Address=@Address,[Reg date]=”“+DateTime.Now.ToShortDateString()+“'WHERE UserName=””+comboBox4.选择EdItem.ToString()+“'”;