今天我用ADO.NET连接数据库时,做增加数据库中的数据的时候,编译的时候没有错,就是运行的时候提示:第一行有语法错误。第一行是引用的using的,怎么会有语法错误呢,就是想不明白了

解决方案 »

  1.   

    这个第一行不是说你代码的第一行,是插入数据库中第一行出现了问题,sql语句错误。
      

  2.   

    Using system.data;
    Using system.data.sqlclient;//sqlserver 数据库
    在检查一下语法
      

  3.   

    是你写的插入语句有问题,检查自己写的sql,微软一般不会出问题,首先考虑自己写的东西
      

  4.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;namespace SQL_1
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "")
                {
                    MessageBox.Show("输入的信息不完整,请重新输入!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    SqlConnection con1 = new SqlConnection("server=(local);user id=chaoqi;pwd=030824;database=MysqlData");
                    StringBuilder strSQL = new StringBuilder();
                    strSQL.Append("insert into 应付帐(供应商名,购入金额,已付款,应付款)");
                    strSQL.Append("values('" + textBox1.Text.Trim().ToString() + "','" + textBox2.Text.Trim().ToString() + "')");
                    strSQL.Append("values('" + textBox3.Text.Trim().ToString() + "','" + textBox4.Text.Trim().ToString() + "')");
                    using (SqlCommand cmd = new SqlCommand(strSQL.ToString(), con1))
                    {
                        con1.Open();
                        cmd.ExecuteNonQuery();
                        //System.Data.SqlClient.SqlException;"第一行:','附近有语法错误。"
                        
                        con1.Close();
                        con1.Dispose();
                    }
                    MessageBox.Show("已成功向数据库表中插入一条记录!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    textBox1.Text = "";
                    textBox2.Text = "";
                    textBox3.Text = "";
                    textBox4.Text = "";
                }
            }        private void button2_Click(object sender, EventArgs e)
            {
                Form3 myf1 = new Form3();
                myf1.Show();
            }        private void button3_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    }
      

  5.   

    strSQL.Append("values('" + textBox1.Text.Trim().ToString() + "','" + textBox2.Text.Trim().ToString() + "')");
      strSQL.Append("values('" + textBox3.Text.Trim().ToString() + "','" + textBox4.Text.Trim().ToString() + "')");
      

  6.   

    一个SQL语句就只有一个insert into table()values()一个values你拼接了两个上去了
      

  7.   

    SqlConnection con1 = new SqlConnection("server=(local);user id=chaoqi;pwd=030824;database=MysqlData");
      StringBuilder strSQL = new StringBuilder();
      strSQL.Append("insert into 应付帐(供应商名,购入金额,已付款,应付款)");
      strSQL.Append("values('" + textBox1.Text.Trim().ToString() + "','" + textBox2.Text.Trim().ToString() + "')");
      strSQL.Append("values('" + textBox3.Text.Trim().ToString() + "','" + textBox4.Text.Trim().ToString() + "')");应该是这里吧
    你insert一条记录
    为什么要写两个Values?
    我写的不知道有没有错
    你试试看strSQL.Append("insert into 应付帐(供应商名,购入金额,已付款,应付款)");
      strSQL.Append("values('" + textBox1.Text.Trim().ToString() + "','" + textBox2.Text.Trim().ToString() + "','" + textBox3.Text.Trim().ToString() + "','" + textBox4.Text.Trim().ToString() + "')");
      

  8.   

    strSQL.Append("values('" + textBox1.Text.Trim().ToString() + "','" + textBox2.Text.Trim().ToString() + "',");
      strSQL.Append("'" + textBox3.Text.Trim().ToString() + "','" + textBox4.Text.Trim().ToString() + "')");修改成这样
      

  9.   

    你调试一下看看。。可以看出sql语句多了一个values
      

  10.   

    作法如下:
    (1)把你的strSQL改如下:
    strSQL.Append("values('" + textBox1.Text.Trim().ToString() + "','" + textBox2.Text.Trim().ToString() + "',");
       strSQL.Append("'" + textBox3.Text.Trim().ToString() + "','" + textBox4.Text.Trim().ToString() + "')");
    (2)debug时点进最后一个Append后的strSQL看,把SQL拷出来在数据库中执行一下,如果ok,则SQL编写没有问题,有则改之.
    (3)有个小见议,你项目中应用参数化,而不用字符串并接这种类型,这代码编写风格很烂~~~~