-1

嗨,我创建了一个私有子,代码必须在其中插入一个 mysql 命令....它确实如此,但最有趣的部分是记录从数据库中消失,它在那里但是我停止调试并重新开始它不存在,甚至不存在在数据库中为什么?

 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)
        Try
            Dim 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

            xConn.Open()
            xComm.ExecuteNonQuery()
            xComm.Dispose()
            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!!!"
        End Try
    End Using
End Sub
4

2 回答 2

1

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

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
于 2012-07-30T01:42:57.270 回答
0

首先,您的命令创建应该使用 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)的副本作为内容并将其复制到构建时的输出目录之前,我们遇到了类似的问题。

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

于 2012-07-30T01:48:37.620 回答