在一个二维数组中插入值.Net使用数据集



我试图从数据库中的表中获取数据,并将其放入数组中,然后将数据输入到数据库中的不同表中。我有困难使用数据集的二维数组。

在下面的代码中,将names(i,0)的值添加到name列中可以完美地工作。然而,当执行下一行时,names(j,0)不起作用,但如果我将其交换为names(0,j),它将在数组中插入第一个值。位置0处的值。我不明白为什么尝试在j处插入值不工作。

Dim count As Integer = ds.Tables(0).Rows.Count
Dim names As String(,) = New String(count - 1, 4) {}
For i As Integer = 0 To count - 1
    For j As Integer = 0 To count - 1
        names(i, j) = ds.Tables(0).Rows(i)(0).ToString()
        Dim myconnectone As New SqlClient.SqlConnection
        myconnectone.ConnectionString = "Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=True"
        Dim mycommandone As SqlClient.SqlCommand = New SqlClient.SqlCommand()
        mycommandone.Connection = myconnectone
        mycommandone.CommandText = "INSERT INTO Matches (HomeTeam,AwayTeam) VALUES (@Hname,@Aname)"
        myconnectone.Open()
        mycommandone.Parameters.Add("@Hname", SqlDbType.NVarChar).Value = names(i, 0)
        mycommandone.Parameters.Add("@Aname", SqlDbType.NVarChar).Value = names(0, j)
        mycommandone.ExecuteNonQuery()
    Next
Next

任何帮助将非常感激!

需要整理,但请尝试

Dim count As Integer = ds.Tables(0).Rows.Count
Dim ColCount As Integer = ds.Tables(0).Columns.Count
Dim names As String(,) = New String(count - 1, 4) {}
For i As Integer = 0 To count - 1
    For j As Integer = 0 To ColCount - 1
        names(i, j) = ds.Tables(0).Rows(i)(j).ToString()
    Next
    Dim myconnectone As New SqlClient.SqlConnection
    myconnectone.ConnectionString = "Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=True"
    Dim mycommandone As SqlClient.SqlCommand = New SqlClient.SqlCommand()
    mycommandone.Connection = myconnectone
    mycommandone.CommandText = "INSERT INTO Matches (HomeTeam,AwayTeam) VALUES (@Hname,@Aname)"
    myconnectone.Open()
    mycommandone.Parameters.Add("@Hname", SqlDbType.NVarChar).Value = names(i, 0)
    mycommandone.Parameters.Add("@Aname", SqlDbType.NVarChar).Value = names(i, 1)
    mycommandone.ExecuteNonQuery()
Next

相关内容

  • 没有找到相关文章