C# VisualStudio错误:类型为';的未处理异常;System.Data.SqlClient.SqlException';发生在System.Data.dll中
尝试将以下信息插入数据库时,我得到“System.Data.SqlClient.SqlException”类型的异常发生在System.Data.dll中 我的代码C# VisualStudio错误:类型为';的未处理异常;System.Data.SqlClient.SqlException';发生在System.Data.dll中,c#,mysql,C#,Mysql,尝试将以下信息插入数据库时,我得到“System.Data.SqlClient.SqlException”类型的异常发生在System.Data.dll中 我的代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using Syste
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace wsiz
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\rf\Documents\wsiz.mdf;Integrated Security=True;Connect Timeout=30");
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO Table (imie,nazwisko,miasto) VALUES ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text +"');";
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("record inserted successfully");
}
}
}
以下是错误cmd.ExecuteNonQuery();
完整异常详细信息
System.Data.SqlClient.SqlException occurred
Class=15
ErrorCode=-2146232060
HResult=-2146232060
LineNumber=1
Message=Incorrect syntax near the keyword 'Table'.
Number=156
Procedure=""
Server=(LocalDB)\MSSQLLocalDB
Source=.Net SqlClient Data Provider
State=1
StackTrace:
w System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
w System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
w System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
w System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
w System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
w System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
w System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
w wsiz.Form1.button1_Click(Object sender, EventArgs e) w c:\users\rf\documents\visual studio 2015\Projects\wsiz\wsiz\Form1.cs:wiersz 28
InnerException:
您的直接错误是因为您正在使用关键字
Table
。请改用[Table]
"INSERT INTO [Table] (imie,nazwisko,miasto) VALUES ..."
在此之后,您应该开始研究查询参数化,以防止sql注入,并帮助调试和扩展。您的直接错误是因为您使用的是关键字
Table
。而不是使用[Table]
"INSERT INTO [Table] (imie,nazwisko,miasto) VALUES ..."
在此之后,您应该开始研究查询参数化,以防止sql注入,并帮助调试和扩展。异常有详细信息和消息。您可以共享吗?我想至少有一个文本框值没有您认为的值。发布完整的异常,包括stacktrace和Internal异常(如果有)。另外,请提供您试图插入的表的表结构。目前,没有足够的信息来解决您的问题。表创建表[dbo]。[table]([Id]INT NOT NULL,[imie]VARCHAR(50)NULL,[nazwisko]VARCHAR(50)NULL,[miasto]VARCHAR(50)NULL,主键群集([Id]ASC))@Defus不要链接到第三方网站,并提供有关错误的信息,不要只在评论中提供,只需将其编辑到您的问题中……还有,您在文本框中输入的数据是什么?您确定要插入整数列的值是实际的整数吗?您的表真的命名为“table”吗"? 如果是这样,请将其包装为非关键字:
[Table]
异常具有详细信息和消息。你能分享一下吗?我猜至少有一个文本框值没有您认为的值。发布完整的异常,包括stacktrace和内部异常(如果有)。另外,请提供要插入的表的表结构。目前,没有足够的信息来解决您的问题。表创建表[dbo]。[Table]([Id]INT NOT NULL,[imie]VARCHAR(50)NULL,[nazwisko]VARCHAR(50)NULL,[miasto]VARCHAR(50)NULL,主键集群([Id]ASC))@Defus不要链接到第三方网站,并提供有关您错误的信息,不要只在评论中提供,只需将其编辑到您的问题中。。。另外,您在文本框中输入了哪些数据?您确定要插入整数列的值是实际整数吗?您的表是否真的命名为“表”?如果是,请将其包装为非关键字:[Table]