How to connect to the mdf file and add rows

Mansour_Dalir 1,591 Reputation points
2024-01-19T18:57:31.5566667+00:00

hi How can this be used? I don't have a problem with creating a table, but when connected to data sheet or adding a row mdf

 Dim con As SqlConnection
        con = New SqlConnection
        con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=" & sttPathDbFile & ";Integrated Security=False;User Instance=False"
       

error {"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"}

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,578 questions
0 comments No comments
{count} votes

Accepted answer
  1. Albert Kallal 4,651 Reputation points
    2024-01-21T18:51:06.4633333+00:00

    No, that option is for what is called a web api, or web based "end point". Hence, that would be (in most cases) some web server, and on that web server, they have SQL server installed, setup and running. And they WILL ALSO have installed a web service system that allows web based calls to the web site, which in turn passes on the SQL statements to the database. So, to setup a connection to SQL server, it is assume that you have SQL setup, and running, and that you have some tables and data already setup in that database. You can then in your project use the connection builder. So, in your project properties page, you can setup a connection to SQL server. CONNECT

    So, say now we create a form, and add a button, and a DataGrid view like this: And the code for the button click can be this:

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim strSQL As String =
                "SELECT FirstName, LastName, City, HotelName, Description
                FROM tblHotels ORDER BY HotelName"
    
            Dim rstHotels As New DataTable
            Using conn As New SqlConnection(My.Settings.Hotels)
                Using cmdSQL As New SqlCommand(strSQL, conn)
                    conn.Open()
                    rstHotels.Load(cmdSQL.ExecuteReader)
                End Using
            End Using
    
            DataGridView1.DataSource = rstHotels
    
        End Sub
    
    

    Note how in above, our connection is the one we created in the project settings page. So, when run, we get this: CONNECT2


0 additional answers

Sort by: Most helpful