Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将空白mysql插入数据库_Mysql_Vb.net - Fatal编程技术网

将空白mysql插入数据库

将空白mysql插入数据库,mysql,vb.net,Mysql,Vb.net,您好,我创建了一个私有子文件,其中的代码必须插入一个mysql命令。。。。但最有趣的是,记录从数据库中消失了 它在那里,但我停止了debbug,重新开始,它不在那里,甚至不在数据库中 为什么? 我修改了代码,看看是否有效 Private Sub gravarAtleta() Dim sqlStatement = "INSERT INTO atl([nome],[morada],[sexo],[datan],[telf],[desporto]) " sqlStat

您好,我创建了一个私有子文件,其中的代码必须插入一个mysql命令。。。。但最有趣的是,记录从数据库中消失了 它在那里,但我停止了debbug,重新开始,它不在那里,甚至不在数据库中 为什么?


我修改了代码,看看是否有效

Private Sub gravarAtleta()

        Dim sqlStatement = "INSERT INTO atl([nome],[morada],[sexo],[datan],[telf],[desporto]) "
        sqlStatement &= "VALUES (@nome, @morada, @sexo, @datan, @telf, @desporto)"

        Using xConn As New SqlConnection(myConnectionString)
            Using xComm As New SqlCommand(sqlStatement, xConn)
                With xComm
                    .CommandType = CommandType.Text
                    .Parameters.AddWithValue("@nome", txtNome.Text)
                    .Parameters.AddWithValue("@morada", txtMorada.Text)
                    .Parameters.AddWithValue("@sexo", ComboSexo.Text)
                    .Parameters.AddWithValue("@datan", CType(txtDataN.Text, DateTime).ToString("yyyy-MM-dd"))
                    .Parameters.AddWithValue("@telf", txtTelemovel.Text)
                    .Parameters.AddWithValue("@desporto", ComboBox1.Text)
                End With
                Try
                    xConn.Open
                    xComm.ExecuteNonQuery
                    Label1.Content = "O atleta " + txtNome.Text + " foi registado!!!"
                Catch ex As SqlException
                    Msgbox (ex.Message)
                    Label1.Content = "Falhou a ligação a base de dados!!!"
                Finally
                    xConn.Close
                End Try
            End Using
        End Using
End Sub

首先,应该使用using语句创建命令;这样,您就不必使用dispose语句,即使数据库生成异常,对象也将被释放。例如:

    Using xComm As New SqlCommand(sqlStatement, xConn)
        With xComm
            .CommandType = CommandType.Text
            .Parameters.AddWithValue("@nome", txtNome.Text)
            .Parameters.AddWithValue("@morada", txtMorada.Text)
            .Parameters.AddWithValue("@sexo", ComboSexo.Text)
            .Parameters.AddWithValue("@datan", CType(txtDataN.Text, DateTime).ToString("yyyy-MM-dd"))
            .Parameters.AddWithValue("@telf", txtTelemovel.Text)
            .Parameters.AddWithValue("@desporto", ComboBox1.Text)

             xConn.Open()

            .ExecuteNonQuery()
        End With
    End Using           
其次,您是否可能在应用程序的部署中包含数据库?我甚至不确定使用MySQL是否可以做到这一点,但我们以前遇到过类似的问题,我们在解决方案中包含一个数据库副本(SQLCE,Access)作为内容,并在构建时将其复制到输出目录


这会造成很大的混乱,因为在以前的调试周期中所做的更改会被新复制的数据库覆盖。

nop try语句错误cath必须在try语句中抱歉,Prince,如果打开Option Explicit,这甚至不会编译,因为xcomm dispose命令无法“查看”xcomm对象(特别是,您将获得未声明的xComm。由于其保护级别,它可能无法访问。“这意味着te insert命令起作用问题是release=,这意味着每次我运行项目时,调试文件夹中的所有文件都会被解决方案目录中的新副本更改?如果必须插入mysql命令,为什么SQL Server连接?
    Using xComm As New SqlCommand(sqlStatement, xConn)
        With xComm
            .CommandType = CommandType.Text
            .Parameters.AddWithValue("@nome", txtNome.Text)
            .Parameters.AddWithValue("@morada", txtMorada.Text)
            .Parameters.AddWithValue("@sexo", ComboSexo.Text)
            .Parameters.AddWithValue("@datan", CType(txtDataN.Text, DateTime).ToString("yyyy-MM-dd"))
            .Parameters.AddWithValue("@telf", txtTelemovel.Text)
            .Parameters.AddWithValue("@desporto", ComboBox1.Text)

             xConn.Open()

            .ExecuteNonQuery()
        End With
    End Using