本文介绍了'nvarchar'附近的语法不正确。必须声明标量变量“@”。在VB.NET中出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我需要将数据从datagrid插入到sql server数据库。但是我的编码有错误。'nvarchar'附近的语法不正确。必须在vb.error中声明标量变量@。这是我的错误...请修复问题.... 和我亲爱的编码显示.. Dim conn As New SqlConnection Dim cmd As SqlCommand Dim sqlstring As String Dim paramdic As New Dictionary(Of String,Object) conn.ConnectionString =数据源= ATHILINGAM \ SQLEXPRESS;初始目录=出勤;综合安全=真; conn.Open() paramdic.Add(@ [Staff Id],value1) paramdic.Add(@ [Staff Name],value2) sqlstring =INSERT INTO dbo.Attend1(field1,field2)VALUES([@Staff Id],@ [Staff Name]) cmd =新SqlCommand(sqlstring,conn) ) 每个keyval为KeyValuePair(Of String,Object)在paramdic中 cmd.Parameters.AddWithValue(keyval.Key,keyval.Value) 下一页 cmd.ExecuteNonQuery() cmd.Dispose() b $ b conn.Close() 我的编码错误是什么.... 我尝试过: i需要将数据从datagrid插入sql server数据库。但是我的编码有错误。'nvarchar'附近的语法不正确。必须在vb.error中声明标量变量@。这是我的错误...请修复问题.... 解决方案 在SQL中'@'表示这是一个变量名称,变量sin SQL不能包含空格。 试试这个: paramdic.Add( @ StaffId, value1) paramdic.Add( @ StaffName, value2) sqlstring = INSERT INTO dbo.Attend1(field1,field2)VALUES(@StaffId,@ StaffName ) i need to insert data from datagrid to sql server database. but my coding have a error."Incorrect syntax near 'nvarchar'. Must declare the scalar variable "@". in vb.error" This is my error...pls fix the problem....And belove my coding showed.. Dim conn As New SqlConnection Dim cmd As SqlCommand Dim sqlstring As String Dim paramdic As New Dictionary(Of String, Object) conn.ConnectionString = "Data Source=ATHILINGAM\SQLEXPRESS;Initial Catalog=Attendance;Integrated Security=True;" conn.Open() paramdic.Add("@[Staff Id]", "value1") paramdic.Add("@[Staff Name]", "value2") sqlstring = "INSERT INTO dbo.Attend1(field1, field2) VALUES ([@Staff Id], @[Staff Name])" cmd = New SqlCommand(sqlstring, conn) For Each keyval As KeyValuePair(Of String, Object) In paramdic cmd.Parameters.AddWithValue(keyval.Key, keyval.Value) Next cmd.ExecuteNonQuery() cmd.Dispose() conn.Close()what is the mistake on my coding....What I have tried:i need to insert data from datagrid to sql server database. but my coding have a error."Incorrect syntax near 'nvarchar'. Must declare the scalar variable "@". in vb.error" This is my error...pls fix the problem.... 解决方案 In SQL '@' says "this is a variable name", and variable sin SQL cannot contain spaces.Try this:paramdic.Add("@StaffId", "value1")paramdic.Add("@StaffName", "value2")sqlstring = "INSERT INTO dbo.Attend1(field1, field2) VALUES (@StaffId, @StaffName)" 这篇关于'nvarchar'附近的语法不正确。必须声明标量变量“@”。在VB.NET中出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-27 13:12