System.data.sqlclient.sqlexception:''''附近的语法不正确。'有谁知道这是什么问题? [英] System.data.sqlclient.sqlexception: 'incorrect syntax near '='.' anyone know what problem is this?

查看:170
本文介绍了System.data.sqlclient.sqlexception:''''附近的语法不正确。'有谁知道这是什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.Data.SqlClient.SqlException

HResult = 0x80131904

消息='='附近的语法不正确。

Source = .Net SqlClient数据提供程序

StackTrace:

在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,Boolean breakConnection,Action`1 wrapCloseInAction)

在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,Boolean breakConnection,Action`1 wrapCloseInAction)

在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,Boolean callerHasConnectionLock,Boolean asyncClose) )

在System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,Boolean& dataReady)

at System。 System.SData.SqlClient.SqlDataReader.get_MetaD上的Data.SqlClient.SqlDataReader.TryConsumeMetaData()

ata()

在System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,String resetOptionsString,Boolean isInternal,Boolean forDescribeParameterEncryption,Boolean shouldCacheForAlwaysEncrypted)

at System .Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async,Int32 timeout,Task& task,Boolean asyncWrite,Boolean inRetry,SqlDataReader ds,Boolean describeParameterEncryptionRequest)

在System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String method,TaskCompletionSource`1 completion, Int32超时,任务和任务,布尔& usedCache,布尔asyncWrite,布尔inRetry)

在System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String method)
(CommandBehavior行为,字符串方法)
System.Data.SqlClient.SqlCommand.ExecuteReader()上的


在C:\ Users \ Owner\source \repos\CAR \ CARA \\ Event5.vb:第72行的CAR.FrmBooking.UsersInformationToolStripMenuItem_Click(Object sender,EventArgs e)中

在System.Windows.Forms.ToolStripItem.RaiseEvent(Object key,EventArgs e)

在System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)

在System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)

在System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)

at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e,ToolStripItemEventType met)

at System。 Windows.Forms.ToolStripItem.FireEvent(EventArgs e,ToolStripItemEventType符合)

在System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)

在System.Windows.Forms .Control.WmMouseUp(消息&安培; m,MouseButtons按钮,Int32点击)

在System.Windows.Forms.Control.WndProc(消息& m)

在System.Windows.Forms.ScrollableControl.WndProc(消息& m)

在System.Windows.Forms.ToolStrip.WndProc(消息& m)

在System.Windows.Forms.MenuStrip.WndProc(消息& m)

在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)

在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)

在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW( MSG& msg)

在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 reason,Int32 pvLoopData)
$ b System.Windows.Forms.Application.ThreadContext.RunMessag上的$ b eLoopInner(Int32 reason,ApplicationContext context)

at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context)

at Microsoft.VisualBasic.ApplicationServices。 Windows.msApplicationBase.OnRun()

at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()

at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run (String [] commandLine)

at CAR.My.MyApplication.Main(String [] Args)in:第81行



我尝试过:



Private Sub UsersInformationToolStripMenuItem_Click(sender As Object,e As EventArgs)处理UsersInformationToolStripMenuItem.Click

FrmUser.Show()

Dim con As New SqlConnection

Dim cmd As New SqlCommand

Dim dr As SqlDataReader





con.Connection String =Data Source = desktop-k1nt5gg\sqlexpress; Initial Catalog = database1; Integrated Security = True

cmd.Connection = con

con.Open()< br $>


cmd.CommandText =SELECT * FROM User_Register WHERE UserID =+ Label58.Text

dr = cmd.ExecuteReader



如果dr.Read那么

FrmUser.Label2.Text = dr(UserID)。ToString()

结束If





结束Sub

解决方案

简单:不要这样做像那样。永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。总是使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  Baker' s Wood ' < span class =code-string>  

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  x';  DROP   MyTable;   -   ' 

哪个SQL看作三个单独的命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  x'; 

完全有效的SELECT

  DROP   TABLE  MyTable; 

完全有效的删除表格通讯和

   -   ' 

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你经常定期备份,不是吗?



当你通过整个应用程序解决了这个问题时,你注意到的问题也会消失。


System.Data.SqlClient.SqlException
HResult=0x80131904
Message=Incorrect syntax near '='.
Source=.Net SqlClient Data Provider
StackTrace:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader()
at CAR.FrmBooking.UsersInformationToolStripMenuItem_Click(Object sender, EventArgs e) in C:\Users\Owner\source\repos\CAR\CAR\Form5.vb:line 72
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.MenuStrip.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at CAR.My.MyApplication.Main(String[] Args) in :line 81

What I have tried:

Private Sub UsersInformationToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UsersInformationToolStripMenuItem.Click
FrmUser.Show()
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim dr As SqlDataReader


con.ConnectionString = "Data Source=desktop-k1nt5gg\sqlexpress;Initial Catalog=database1;Integrated Security=True"
cmd.Connection = con
con.Open()

cmd.CommandText = "SELECT * FROM User_Register WHERE UserID =" + Label58.Text
dr = cmd.ExecuteReader

If dr.Read Then
FrmUser.Label2.Text = dr("UserID").ToString()
End If


End Sub

解决方案

Simple: don't do it like that. Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:

SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

A perfectly valid SELECT

DROP TABLE MyTable;

A perfectly valid "delete the table" command

--'

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And when you have fixed that through your whole app, the problem you have noticed will have vanished as well...


这篇关于System.data.sqlclient.sqlexception:''''附近的语法不正确。'有谁知道这是什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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