vb 2010 mysql插入记录操作插入空白记录,但不插入数据

vb 2010 mysql插入记录操作插入空白记录,但不插入数据,mysql,vb.net,insert,Mysql,Vb.net,Insert,我试图在VB2010中开发一个实用程序,它将Datagridview控件中的值存储到mysql表中 台阶 1) Datagrid单元格值存储在数组中 2) 使用数组值。AddWithValue()函数 当前状态:仅插入空白记录。数据丢失 请参考以下代码 Function insertRec() 'dgExistingProject is datagridview control embedded on the form Dim ServerS

我试图在VB2010中开发一个实用程序,它将Datagridview控件中的值存储到mysql表中

台阶 1) Datagrid单元格值存储在数组中 2) 使用数组值。AddWithValue()函数

当前状态:仅插入空白记录。数据丢失

请参考以下代码

         Function insertRec()
         'dgExistingProject is datagridview control embedded on the form
         Dim ServerString As String = "Server=pnserver;User ID=root;
         Password=; Database=prayer_net"
         Dim Arr(RCount) As String
         Dim SQLConn As MySqlConnection = New MySqlConnection
         SQLConn.ConnectionString = ServerString
         dim I as Integer
         For I = 0 To RCount
                Arr(I) = dgExistingProject.Rows(I).Cells(1).Value
         Next
            Try
                Dim dt As Date = DateTime.Now.ToString("yyyy/MM/dd 
                      HH:mm:ss")
                Dim projName as Integer=Arr(4)
                Using sqlCommand As New MySqlCommand
                    With sqlCommand
                        .CommandText = "INSERT INTO dyn_dwg_register 
                     (file_name,file_location,dwg_description,  
                     project_id,created_by, created_on)" & _
                                      " values (@file_name, @file_location, @dwg_description, @project_id, @created_by, @created_on)"
                        .CommandType = CommandType.Text
                        .Connection = SQLConn
                        .Parameters.AddWithValue("@file_name", Arr(0))
                        .Parameters.AddWithValue("@file_location", Arr(1))
                        .Parameters.AddWithValue("@dwg_description", Arr(2))
                        .Parameters.AddWithValue("@project_id", ProjName)
                        .Parameters.AddWithValue("@created_by", Arr(4))
                        .Parameters.AddWithValue("@created_on", dt)
                    End With
                    Try
                        SQLConn.Open()
                        If SQLConn.State = ConnectionState.Open Then
                            Dim ID As Integer = sqlCommand.ExecuteNonQuery()
                        End If
                    Catch ex As Exception
                        MsgBox(ex.Message.ToString)
                    Finally
                        SQLConn.Close()
                    End Try
                End Using
            Catch e As Exception
                  MsgBox (e.Message.ToString)
            End Try
End Function
Rajendra Nadkar

我用C#编写了一个代码,我必须将数据网格中的数据存储到SQL数据库中。我使用了以下代码。我想这会对你有帮助

string sqlquery = "UPDATE TableName set ColumnName1=@param1 where ColumnName2=@param2";
using (OleDbCommand Cmd = new OleDbCommand(sqlQuery, dbCon))
{
Cmd.Parameters.AddWithValue("@param1", Convert.ToDouble(dataGridView1[1, index].Value));
Cmd.Parameters.Add(new OleDbParameter("@param2", OleDbType.VarWChar)).Value = "somevalue";
            updateDbCmd.ExecuteNonQuery();
            updateDbCmd.Parameters.Clear();
}

有趣的是,我把域名前缀@改成了?事情变得很顺利。在2月11日早些时候的一个问题中,Stelian Matei提出了这一编辑,但后来Giezel Esteves将其推翻。(用户1176607在2月10日提出的原始问题)

我想我需要更彻底地参考文档。不管怎样,如果你或任何对它感兴趣的人,我在这里给出了工作代码

        Dim qry As String = "INSERT INTO dyn_dwg_register (`file_name` , `file_location`, `dwg_description`, `project_id`, `created_by`)" & _
                                  " values (?file_name, ?file_location, ?dwg_description, ?project_id, ?created_by)"
        Dim sqlCommand As MySqlCommand
        sqlCommand = New MySqlCommand(qry, SQLConn)
        sqlCommand.CommandType = CommandType.Text
        With sqlCommand
            .Parameters.AddWithValue("?file_name", Arr(0))
            .Parameters.AddWithValue("?file_location", Arr(1))
            .Parameters.AddWithValue("?dwg_description", Arr(2))
            .Parameters.AddWithValue("?project_id", ProjName)
            .Parameters.AddWithValue("?created_by", Arr(2))
        End With

很明显,我犯了一些错误,但却变得一片空白。谢谢你的帮助。将尝试使用该语法[Cmd.Parameters.AddWithValue(“@param1”,Convert.ToDouble(dataGridView1[1,index.Value));]out。同时检查数组是否具有这些值。可能数组是空的。基本上我可以更新记录,当我们插入新记录时会出现问题。数组不是空的。通过使用sqlCommand在中添加msgbox来验证。。。以block.Parameters.AddWithValue(“@file_name”,MySqlDbType.VarChar)结尾。Value=Arr(0)。Parameters.AddWithValue(“@file_location”,MySqlDbType.VarChar)。Value=Arr(1)。Parameters.AddWithValue(@dwg_description,MySqlDbType.VarChar)。Value=Arr(2)。Parameters.AddWithValue(“@project_id”,MySqlDbType.Int32.Value=ProjName.Parameters.AddWithValue(“@created_by”,MySqlDbType.VarChar)。Value=Arr(4)