简体   繁体   English

如何连接数据库

[英]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. 我已经设法使用sqlexpress使用localhost连接到数据库,所以我的连接字符串正确,但是我托管在123-reg上,无法连接到我在其服务器上创建的数据库。 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. 我尝试了SQL插入和选择语句,以查看在我的网站实时查看时我的网站是否可以连接到它,但是它没有连接。 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: 123-reg给了我一个连接字符串,可以插入到我的webconfig页面中:

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

I have tried to access this on an aspx page using: 我试图使用以下方法在aspx页面上访问此文件:

    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 , 他们给您: Provider SqlOleDb

This means that you should use the OleDb classes ( OleDbConnection, OleDbCommand etc.) not the classes in the SqlClient namespace ( SqlConnection, SqlCommand etc). 这意味着您应该使用OleDb类( OleDbConnection, OleDbCommand等),而不是SqlClient名称空间中的类( SqlConnection, SqlCommand等)。

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) 如果这是真的,并且它们没有提供更新的Sql Native Client的替代方法,我认为您必须更改代码才能使用这些类(或更改提供程序)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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