c# OleDb 异常中 INSERT INTO 语句中的语法错误无法发现错误 [英] Syntax error in INSERT INTO statement in c# OleDb Exception cant spot the error

查看:65
本文介绍了c# OleDb 异常中 INSERT INTO 语句中的语法错误无法发现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

完全重复的语法C# 中 INSERT INTO 语句中的错误oledb?

我无法发现错误.请帮忙.由于语法错误,存在 OleDb 异常.INSERT INTO 语句中的语法错误 OleDb 异常未处理.

Hi I cant spot the error. Please help. There is an OleDb Exception due to a Syntax Error. Syntax error in INSERT INTO statement OleDb Exception is unhandled.

    private OleDbConnection myCon;

    public Form1()
    {
        InitializeComponent();
        myCon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C.mdb");
    }

private void insertuser_Click(object sender, EventArgs e)
    {
        try
        {
            OleDbCommand cmd = new OleDbCommand();
            myCon.Open();
            cmd.Connection = myCon;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "INSERT INTO User ([UserID], [Forename], [Surname], [DateOfBirth], [TargetWeight], [TargetCalories], [Height]) Values ('" + userid.Text + "' , '" + fname.Text + "' , '" + sname.Text + "' , '" + dob.Text + "' , '" + tarweight.Text + "' , '" + tarcal.Text + "' , '" + height.Text + "')";


            cmd.ExecuteNonQuery();
            myCon.Close();
        }
        catch (Exception ex) { MessageBox.Show(ex.ToString()); }



    }

推荐答案

您尝试插入的值是什么?高度是否以英尺和英寸 (5'10") 为单位?在这种情况下,您将关闭字符串 (') 并出现语法错误.

What are the values you're attempting to insert? Is height perhaps in feet and inches (5'10")? In which case you'll have closed the string (') and will have a syntax error.

我完全同意@Brennan Vincent.构建原始 SQL 不是前进的方向.

And I agree wholeheartedly with @Brennan Vincent. Constructing raw SQL is not the way forward.

这篇关于c# OleDb 异常中 INSERT INTO 语句中的语法错误无法发现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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