Data insertion into sql vb.net database is not saved

I have a strange problem: I am trying to make a basic procedure for inserting data into a database, and I do not know what happens, but it does not work.

The thing is, I have a database called prova3, which is a SQL Server 2012 database created using vb.net 2013, with a table tabela. I created a dataset to see the connection string that is stored in app.config. The line is the same. I am not getting an error, but the data is not inserted. When I go to the server explorer to see the data in the table, it is empty.

I think the data was saved elsewhere, but I don’t know how to fix it, because I think I encode it correctly. This is for vb.net, but I made the same code for asp.net, and it works. Weird

could you help me?

In the form, it only controls text field1 and button1. No more code.

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim ImageUrlSt As String
    Dim command1 As New SqlCommand
    Dim con As New SqlConnection
    ImageUrlSt = TextBox1.Text
    Try
        con.ConnectionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Prova3.mdf;Integrated Security=True"
        con.Open()
        command1.Connection = con
        command1.CommandText = "INSERT INTO Tabela (imageurl) VALUES (@imageurlst)"
        command1.Parameters.Add(New SqlParameter("@imageurlst", ImageUrlSt))
        command1.ExecuteNonQuery()
        MsgBox("News Saved Succesfully")
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        con.Close()
    End Try
End Sub
+4
source share
1 answer

Sometimes, |DataDirectory|the path is problematic when debugging. Have you checked a copy of db on \bin\debug?

Theres Copy to Output Directory, - Copy if newer ( .mdf .mdb, Copy always). MSDN, , . , , THAT .

, Visual Studio , Copy to Output Directory Do not copy. , . , : , .

- ConnectionString, con.ConnectionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\MyProjectFolder\Prova3.mdf;Integrated Security=True"

+2

Source: https://habr.com/ru/post/1533483/


All Articles