简体   繁体   中英

how to connect to database

I have managed to connect to a database using localhost using sqlexpress, so my connection string is correct, however I am hosted on 123-reg and cannot connect to the database I created on their server. I have tried an SQL insert and select statements to see if my website can connect to it when viewed on my website live, but it does not connect. It is the connection that fails, I am not sure if I am linking to the server correctly.

123-reg gave me a connection string to insert into my webconfig page:

<add key="ConnectionString" value="*****; Database=*****; User Id=*****; Password=*****" />

I have tried to access this on an aspx page using:

    Dim con As New SqlConnection
    Dim cmd As New SqlCommand
    Try
        con.ConnectionString = "Provider=*****;DataSource=*****; Initial Catalog=*****; User Id=*****; Password=*****;"

        con.Open()
        cmd.Connection = con
        cmd.CommandText = "insert into test (note) values ('works')"
        cmd.ExecuteNonQuery()


    Catch ex As Exception
        MsgBox("Error while inserting record on table..." & ex.Message)
    Finally
        con.Close()
        Label1.Text = "works"
    End Try

They give you: Provider SqlOleDb ,

This means that you should use the OleDb classes ( OleDbConnection, OleDbCommand etc.) not the classes in the SqlClient namespace ( SqlConnection, SqlCommand etc).

If this is true and they do not offer an alternative to a more updated Sql Native Client, I think you have to change your code to use these classes (or change provider)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM