错误:公共无效InsertRecord(SqlCommand cmd)varchar附近的语法不正确.必须清除定标器变量'@' [英] Error : Public void InsertRecord(SqlCommand cmd)Incorrect syntax near varchar.Must declear scaler variable '@'

查看:65
本文介绍了错误:公共无效InsertRecord(SqlCommand cmd)varchar附近的语法不正确.必须清除定标器变量'@'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string custnm = txtCustNm.Text.Trim();
                string address = txtAddr.Text.Trim();
                string city = txtCity.Text.Trim();
                string contactno = txtContact.Text.Trim();
                string email = txtEmail.Text.Trim();

                string insertQuery = "INSERT INTO Customer([Customer Name],Address,City,[Contact No.],[Email Id]) VALUES (@[Customer Name],@Address,@City,@[Contact No.],@[Email Id])";
                SqlCommand cmd = new SqlCommand(insertQuery);
                cmd.Parameters.Add("@[Customer Name]", SqlDbType.VarChar, 50).Value = custnm;
                cmd.Parameters.Add("@Address", SqlDbType.VarChar, 50).Value = address;
                cmd.Parameters.Add("@City", SqlDbType.VarChar, 50).Value = city;
                cmd.Parameters.Add("@[Contact No.]", SqlDbType.BigInt).Value = contactno;
                cmd.Parameters.Add("@[Email Id]", SqlDbType.VarChar, 50).Value = email;


                dbfun.InsertRecord(cmd);
               // MessageBox.Show("Record Inserted Successfully");

                LoadData();

            }
            catch (Exception ex)
            {
                MessageBox.Show("private void btnSubmit_Click(object sender, EventArgs e)" + ex.Message);
            }
            finally
            {

            }

推荐答案

我相信您不能在空格中使用@parameters,也不能放在方括号中.
请尝试以下操作:
I believe you can''t use @parameters with spaces in them and put square brackets also.
Try the following:
string insertQuery = "INSERT INTO Customer([Customer Name],Address,City,[Contact No.],[Email Id]) VALUES (@CustomerName,@Address,@City,@ContactNo,@EmailId)";
                SqlCommand cmd = new SqlCommand(insertQuery);
                cmd.Parameters.Add("@CustomerName", SqlDbType.VarChar, 50).Value = custnm;
                cmd.Parameters.Add("@Address", SqlDbType.VarChar, 50).Value = address;
                cmd.Parameters.Add("@City", SqlDbType.VarChar, 50).Value = city;
                cmd.Parameters.Add("@ContactNo", SqlDbType.BigInt).Value = contactno;
                cmd.Parameters.Add("@EmailId", SqlDbType.VarChar, 50).Value = email;


还要尝试在列名称中也不要使用''.''.


Also try not to use ''.'' in the column names also.


这篇关于错误:公共无效InsertRecord(SqlCommand cmd)varchar附近的语法不正确.必须清除定标器变量'@'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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