利用ADO.NET连接数据库的问题 今天我用ADO.NET连接数据库时,做增加数据库中的数据的时候,编译的时候没有错,就是运行的时候提示:第一行有语法错误。第一行是引用的using的,怎么会有语法错误呢,就是想不明白了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个第一行不是说你代码的第一行,是插入数据库中第一行出现了问题,sql语句错误。 Using system.data;Using system.data.sqlclient;//sqlserver 数据库在检查一下语法 是你写的插入语句有问题,检查自己写的sql,微软一般不会出问题,首先考虑自己写的东西 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(); } }} strSQL.Append("values('" + textBox1.Text.Trim().ToString() + "','" + textBox2.Text.Trim().ToString() + "')"); strSQL.Append("values('" + textBox3.Text.Trim().ToString() + "','" + textBox4.Text.Trim().ToString() + "')"); 一个SQL语句就只有一个insert into table()values()一个values你拼接了两个上去了 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() + "')"); strSQL.Append("values('" + textBox1.Text.Trim().ToString() + "','" + textBox2.Text.Trim().ToString() + "',"); strSQL.Append("'" + textBox3.Text.Trim().ToString() + "','" + textBox4.Text.Trim().ToString() + "')");修改成这样 你调试一下看看。。可以看出sql语句多了一个values 作法如下:(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)有个小见议,你项目中应用参数化,而不用字符串并接这种类型,这代码编写风格很烂~~~~ office visio 里面的那个保存功能怎么写? 讨论,如何能阻止反射呢? 简单正则 c#用的String问题 请问 n 个 2 相乘怎么表示???在线等待,马上给分!! 假如我把字符串(AAA)以Image的数据类型存入道SQL数据库中,再从数据库中读出该字符串(AAA),把该字符串(AAA)添加到ComboBox控件的It C#学习方法 高分求SkinCrafte注册码,分不是问题 怎样检测网络是否通畅? 如何禁止用户在短时间内提交2次表单 WINDOWS窗体应用程序与WPF应用程序有啥区别? JAVA代码转成C#
Using system.data.sqlclient;//sqlserver 数据库
在检查一下语法
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();
}
}
}
strSQL.Append("values('" + textBox3.Text.Trim().ToString() + "','" + textBox4.Text.Trim().ToString() + "')");
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() + "')");
strSQL.Append("'" + textBox3.Text.Trim().ToString() + "','" + textBox4.Text.Trim().ToString() + "')");修改成这样
(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)有个小见议,你项目中应用参数化,而不用字符串并接这种类型,这代码编写风格很烂~~~~