尝试更新MS Access时出现语法错误 [英] Syntax error while trying to UPDATE MS Access

查看:78
本文介绍了尝试更新MS Access时出现语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它说语法错误,但我的一句话似乎没问题,这里有什么问题吗?



  public   void  updatedata( string  EN_NO, string  MName, string  MAddress, string 社会, string 组, string 联系方式)
{

OleDbCommand updatenow = new OleDbCommand();

con.Open();
updatenow.Connection = con;
updatenow.CommandText = update成员集MName =?,MAddress = ?,社会= ?,组= ?,Contact =?其中EN_NO =?;


updatenow.Parameters.Add( EN_NO, OleDbType.VarWChar);
updatenow.Parameters [ EN_NO]。Value = EN_NO;

updatenow.Parameters.Add( MName,OleDbType.VarWChar) ;
updatenow.Parameters [ MName]。Value = MName;

updatenow.Parameters.Add( MAddress,OleDbType.VarWChar) ;
updatenow.Parameters [ MAddress]。Value = MAddress;

updatenow.Parameters.Add( 社会,OleDbType.VarWChar) ;
updatenow.Parameters [ 社会]。价值=社会;

updatenow.Parameters.Add( Group,OleDbType.VarWChar) ;
updatenow.Parameters [ Group]。Value = Group;

updatenow.Parameters.Add( 联系人,OleDbType.VarWChar) ;
updatenow.Parameters [ 联系人]。值=联系人;

尝试
{
int count = updatenow .ExecuteNonQuery();

if (count > 0
{
MessageBox.Show( 成功记录更新的 Methodist Church,MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
catch (OleDbException e)
{
MessageBox.Show(e.Message) ;
}
con.Close();

}


private void btnupdate_Click( object sender,EventArgs e)
{
updatedata(cmbno.Text,txtname.Text,txtaddress.Text,txtsociety.Text, txtgroup.Text,txtcontact.Text);
}

解决方案

似乎 OLE DB .NET Provider 不支持命名参数。



试试这个:

 updatenow.Connection = con; 
updatenow.CommandText = update成员集MName =?,MAddress = ?,社会= ?,组= ?,Contact =?其中EN_NO =?;


updatenow.Parameters.Add( MName, OleDbType.VarWChar);
updatenow.Parameters [ MName]。Value = MName;

updatenow.Parameters.Add( MAddress,OleDbType.VarWChar) ;
updatenow.Parameters [ MAddress]。Value = MAddress;

updatenow.Parameters.Add( 社会,OleDbType.VarWChar) ;
updatenow.Parameters [ 社会]。价值=社会;

updatenow.Parameters.Add( Group,OleDbType.VarWChar) ;
updatenow.Parameters [ Group]。Value = Group;

updatenow.Parameters.Add( 联系人,OleDbType.VarWChar) ;
updatenow.Parameters [ 联系人]。值=联系人;

updatenow.Parameters.Add( EN_NO,OleDbType.VarWChar) ;
updatenow.Parameters [ EN_NO]。Value = EN_NO;





参见OLEDBCommand.Parameters property [ ^ ]

引用:

当设置CommandType时,OLE DB .NET提供程序不支持将参数传递给SQL语句或OleDbCommand调用的存储过程的命名参数到文本。在这种情况下,必须使用问号(?)占位符





@OriginalGriff:感谢您的提示?位置参数。从未使用 OLDDBCommand ,然后在帮助文件中查找并学习。


因为问号是位置参数:所以他们按添加参数值的顺序替换。由于您在语句中最后添加EN_NO,并且首先在参数列表中添加EN_NO,因此您将要搞砸了。使用命名参数:它可以节省计数!

 updatenow.CommandText =  更新成员集MName = @ MName,MAddress = @ MAddress,社会= @社会,组= @组,联系人= @联系人,其中EN_NO = @ ENNO; 

updatenow.Parameters.AddWithValue( @ EN_NO,EN_NO);
updatenow.Parameters.AddWithValue( @ MName,MName);
updatenow.Parameters.AddWithValue( @ MAddress,MAddress);
updatenow.Parameters.AddWithValue( @ Society,Society);
updatenow.Parameters.AddWithValue( @ Group,Group);
updatenow.Parameters.AddWithValue( @ Contact,Contact);


感谢每一个帮助我的人,我通过改变所有独特的名字来解决问题:D



 datenow.Connection = con; 
updatenow.CommandText = update成员集mname =?,maddress =?,msociety = ?, mgroup = ?,mcontact =?where envno =?;


updatenow.Parameters。添加 mname,OleDbType.VarWChar);
updatenow.Parameters [ mname]。Value = memname;

updatenow.Parameters。添加 maddress,OleDbType.VarWChar);
updatenow.Parameters [ maddress]。Value = memadderss;

updatenow.Parameters。添加 msociety,OleDbType.VarWChar);
updatenow.Parameters [ msociety]。值= memsociety;

updatenow.Parameters。添加 mgroup,OleDbType.VarWChar);
updatenow.Parameters [ mgroup]。Value = memgroup;

updatenow.Parameters。添加 mcontact,OleDbType.VarWChar);
updatenow.Parameters [ mcontact]。Value = memcontact;

updatenow.Parameters。添加 envno,OleDbType.VarWChar);
updatenow.Parameters [ envno]。Value = memEN_NO;


It says syntax error but my piece of cording seems to be ok, is there any thing wrong here?

public void updatedata(string EN_NO, string MName, string MAddress, string Society, string Group, string Contact)
        {

            OleDbCommand updatenow = new OleDbCommand();

            con.Open();
            updatenow.Connection = con;
            updatenow.CommandText = "update Member set MName=?, MAddress=?, Society=?, Group=?, Contact=? where EN_NO=?";


            updatenow.Parameters.Add("EN_NO", OleDbType.VarWChar);
            updatenow.Parameters["EN_NO"].Value = EN_NO;

            updatenow.Parameters.Add("MName", OleDbType.VarWChar);
            updatenow.Parameters["MName"].Value = MName;

            updatenow.Parameters.Add("MAddress", OleDbType.VarWChar);
            updatenow.Parameters["MAddress"].Value = MAddress;

            updatenow.Parameters.Add("Society", OleDbType.VarWChar);
            updatenow.Parameters["Society"].Value = Society;

            updatenow.Parameters.Add("Group", OleDbType.VarWChar);
            updatenow.Parameters["Group"].Value = Group;

            updatenow.Parameters.Add("Contact", OleDbType.VarWChar);
            updatenow.Parameters["Contact"].Value = Contact;

            try
            {
                int count = updatenow.ExecuteNonQuery();

                if (count > 0)
                {
                    MessageBox.Show("Successfully record updated", "Methodist Church", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (OleDbException e)
            {
                MessageBox.Show(e.Message);
            }
            con.Close();

        }


        private void btnupdate_Click(object sender, EventArgs e)
        {
            updatedata(cmbno.Text, txtname.Text, txtaddress.Text, txtsociety.Text, txtgroup.Text, txtcontact.Text);
        }

解决方案

It appears that OLE DB .NET Provider does not support named parameters.

Try this:

updatenow.Connection = con;
updatenow.CommandText = "update Member set MName=?, MAddress=?, Society=?, Group=?, Contact=? where EN_NO=?";


updatenow.Parameters.Add("MName", OleDbType.VarWChar);
updatenow.Parameters["MName"].Value = MName;

updatenow.Parameters.Add("MAddress", OleDbType.VarWChar);
updatenow.Parameters["MAddress"].Value = MAddress;

updatenow.Parameters.Add("Society", OleDbType.VarWChar);
updatenow.Parameters["Society"].Value = Society;

updatenow.Parameters.Add("Group", OleDbType.VarWChar);
updatenow.Parameters["Group"].Value = Group;

updatenow.Parameters.Add("Contact", OleDbType.VarWChar);
updatenow.Parameters["Contact"].Value = Contact;

updatenow.Parameters.Add("EN_NO", OleDbType.VarWChar);
updatenow.Parameters["EN_NO"].Value = EN_NO;



See OLEDBCommand.Parameters property[^]

Quote:

The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used



@OriginalGriff: Thanks for the tip about ? positional parameters. Never used OLDDBCommand before so looked it up in the Help file and learned.


Because the question mark is a position parameter: so they are replaced in the order that the parameter values are added. Since you add the EN_NO last in the statement and first in the parameters list, you are going to mess things up. Use named parameters instead: it saves counting!

updatenow.CommandText = "update Member set MName=@MName, MAddress=@MAddress, Society=@Society, Group=@Group, Contact=@Contact where EN_NO=@ENNO";

updatenow.Parameters.AddWithValue("@EN_NO", EN_NO);
updatenow.Parameters.AddWithValue("@MName", MName);
updatenow.Parameters.AddWithValue("@MAddress", MAddress);
updatenow.Parameters.AddWithValue("@Society", Society);
updatenow.Parameters.AddWithValue("@Group", Group);
updatenow.Parameters.AddWithValue("@Contact", Contact);


thanks for every one who helped me any way i sought out the problem by changing all the unique name :D

datenow.Connection = con;
            updatenow.CommandText = "update Member set mname=?, maddress=?, msociety=?, mgroup=?, mcontact=? where envno=?";


            updatenow.Parameters.Add("mname", OleDbType.VarWChar);
            updatenow.Parameters["mname"].Value = memname;

            updatenow.Parameters.Add("maddress", OleDbType.VarWChar);
            updatenow.Parameters["maddress"].Value = memadderss;

            updatenow.Parameters.Add("msociety", OleDbType.VarWChar);
            updatenow.Parameters["msociety"].Value = memsociety;

            updatenow.Parameters.Add("mgroup", OleDbType.VarWChar);
            updatenow.Parameters["mgroup"].Value = memgroup;

            updatenow.Parameters.Add("mcontact", OleDbType.VarWChar);
            updatenow.Parameters["mcontact"].Value = memcontact;

            updatenow.Parameters.Add("envno", OleDbType.VarWChar);
            updatenow.Parameters["envno"].Value = memEN_NO;


这篇关于尝试更新MS Access时出现语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆