我正在尝试将数据网格视图行添加到sql表中,但this error正在显示,但实际数据已保存:
参数化查询'(@ Name1 nvarchar(4000),@ Dad nvarchar(4000),@ Gender nvarchar(4000'需要参数'@ Name1', 没有提供。
我做了很多研究,有人说添加dbNull值并没有发生任何事情。我也阅读了MSN文档,但还不够了解。
cmd.CommandText = "Insert into Member values('" & txt_fserial_no.Text & "',@Name1,@DOB,@Gender,@Dad,@Relation,@NRC,@Citizen,@Job,@Race,@Country,@Religion)"
cmd.CommandType = CommandType.Text
' Dim adapFam As New SqlDataAdapter'
cmd.Parameters.Add("@Name1", SqlDbType.VarChar)
cmd.Parameters.Add("@Dad", SqlDbType.VarChar)
cmd.Parameters.Add("@Gender", SqlDbType.VarChar)
cmd.Parameters.Add("@DOB", SqlDbType.Date)
cmd.Parameters.Add("@Job", SqlDbType.VarChar)
cmd.Parameters.Add("@Race", SqlDbType.VarChar)
cmd.Parameters.Add("@Country", SqlDbType.VarChar)
cmd.Parameters.Add("@Religion", SqlDbType.VarChar)
cmd.Parameters.Add("@Relation", SqlDbType.VarChar)
cmd.Parameters.Add("@Citizen", SqlDbType.VarChar)
cmd.Parameters.Add("@NRC", SqlDbType.VarChar)
For i As Integer = 0 To datagrid_preview.Rows.Count - 1
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("@Name1", datagrid_preview.Rows(i).Cells(0).Value)
cmd.Parameters.AddWithValue("@Dad", datagrid_preview.Rows(i).Cells(1).Value)
cmd.Parameters.AddWithValue("@Gender", datagrid_preview.Rows(i).Cells(2).Value)
cmd.Parameters.AddWithValue("@DOB", datagrid_preview.Rows(i).Cells(3).Value)
cmd.Parameters.AddWithValue("@Job", datagrid_preview.Rows(i).Cells(4).Value)
cmd.Parameters.AddWithValue("@Race", datagrid_preview.Rows(i).Cells(5).Value)
cmd.Parameters.AddWithValue("@Country", datagrid_preview.Rows(i).Cells(6).Value)
cmd.Parameters.AddWithValue("@Religion", datagrid_preview.Rows(i).Cells(7).Value)
cmd.Parameters.AddWithValue("@Relation", datagrid_preview.Rows(i).Cells(8).Value)
cmd.Parameters.AddWithValue("@Citizen", datagrid_preview.Rows(i).Cells(9).Value)
cmd.Parameters.AddWithValue("@NRC", datagrid_preview.Rows(i).Cells(10).Value)
cmd.Connection = conn
cmd.ExecuteNonQuery()
'adapFam.InsertCommand.ExecuteNonQuery()'
Next
conn.Close()`][1]
答案 0 :(得分:1)
我认为这应该有效:
cmd.CommandText = "Insert into Member(id,Name1,Dad,Gender,DOB,Job,Race,Country,Religion,Relation,Citizen,NRC) values(@id,@Name1,@DOB,@Gender,@Dad,@Relation,@NRC,@Citizen,@Job,@Race,@Country,@Religion)"
cmd.CommandType = CommandType.Text
cmd.Connection = conn
For i As Integer = 0 To datagrid_preview.Rows.Count - 1
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("@id", txt_fserial_no.Text)
cmd.Parameters.AddWithValue("@Name1", datagrid_preview.Rows(i).Cells(0).Value)
cmd.Parameters.AddWithValue("@Dad", datagrid_preview.Rows(i).Cells(1).Value)
cmd.Parameters.AddWithValue("@Gender", datagrid_preview.Rows(i).Cells(2).Value)
cmd.Parameters.AddWithValue("@DOB", datagrid_preview.Rows(i).Cells(3).Value)
cmd.Parameters.AddWithValue("@Job", datagrid_preview.Rows(i).Cells(4).Value)
cmd.Parameters.AddWithValue("@Race", datagrid_preview.Rows(i).Cells(5).Value)
cmd.Parameters.AddWithValue("@Country", datagrid_preview.Rows(i).Cells(6).Value)
cmd.Parameters.AddWithValue("@Religion", datagrid_preview.Rows(i).Cells(7).Value)
cmd.Parameters.AddWithValue("@Relation", datagrid_preview.Rows(i).Cells(8).Value)
cmd.Parameters.AddWithValue("@Citizen", datagrid_preview.Rows(i).Cells(9).Value)
cmd.Parameters.AddWithValue("@NRC", datagrid_preview.Rows(i).Cells(10).Value)
cmd.ExecuteNonQuery()
Next
答案 1 :(得分:0)
将datatable定义为global,用于将gridview raw插入到SQL Server表中,同样是用户绑定gridview。
将dt
直接传递给SQL Server存储过程。
在SQL Server中创建一个函数:
CREATE PROCEDURE [dbo].[Insert_xyz]
@tblxyz xyzType READONLY
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO xyz(Id, Name, Country)
SELECT Id, Name, Country
FROM @tblxyz
END
这是VB的代码:
If dt.Rows.Count > 0 Then
Dim cons As String = ConfigurationManager.ConnectionStrings("xyz").ConnectionString
Using con As New SqlConnection(cons)
Using cmd As New SqlCommand("Insert_xyz")
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = con
cmd.Parameters.AddWithValue("@tblxyz", dt)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End If
抱歉我的英语不好:)我尽力解释你。 :)