将数据网格视图行添加到 vb.net 中的 sql 个表
adding data grid view rows to sql tables in vb.net
我正在尝试将数据网格视图行添加到 sql table 但 this error 正在显示,尽管实际数据已保存:
The parameterized query '(@Name1 nvarchar(4000),@Dad
nvarchar(4000),@Gender nvarchar(4000' expects the parameter '@Name1',
which was not supplied.
我做了很多研究,有人说要添加 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]
我认为这应该可行:
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
定义数据table 为全局数据,用于将您的 gridview raw 插入 SQL 服务器 table 以及用于绑定您的 gridview 的用户。
将 dt
直接传递给您的 SQL 服务器存储过程。
在 SQL 服务器中创建函数:
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
抱歉我的英语不好 :) 我尽力向您解释了。 :)
我正在尝试将数据网格视图行添加到 sql table 但 this error 正在显示,尽管实际数据已保存:
The parameterized query '(@Name1 nvarchar(4000),@Dad nvarchar(4000),@Gender nvarchar(4000' expects the parameter '@Name1', which was not supplied.
我做了很多研究,有人说要添加 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]
我认为这应该可行:
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
定义数据table 为全局数据,用于将您的 gridview raw 插入 SQL 服务器 table 以及用于绑定您的 gridview 的用户。
将 dt
直接传递给您的 SQL 服务器存储过程。
在 SQL 服务器中创建函数:
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
抱歉我的英语不好 :) 我尽力向您解释了。 :)