简体   繁体   中英

VB.net Connect to a database

I am using visual studio 2010 to write some VB scripts that reads some txt files and inputs the results into a Database. the database I am connecting too is a Microsoft SQL server 2008 service pack 2. I am trying to use a connection string that specifies Data Source, Database, user ID and password. which then gets called by

Using SQLConnection As New SqlConnection(connectionString)
        Try
            SQLConnection.Open()

However I keep getting that the database cannot be found, or that the connection cannot be made. Thanks in advance for any advice or links to any questions I might be duplicating.

Cheers.

The connection string is not correct, or the database really cannot be reached from your system.

You can get information on connection strings here: http://www.connectionstrings.com/

If the database is not on your system, check the firewalls on your system and the server to see if any ports need to be opened. Also, check the use of the server network name vs. IP address, or localhost vs. 127.0.0.1 in the server string.

If you have your SQL server on the same machine as Visual Studio. Try this connectionstring.

"Data Source=.;Initial Catalog=your_database_Name;Integrated Security=True"

"Data Source=." - This will go to your own computer. Same as localhost. "Initial Catalog=your_database_Name" - Will use the database of this name in your SQL server. "Integrated Security=True" - will use your own user to connect to the database. This requires that you have access to the sql server. If you are admin on your machine, this would work just fine.

try this if you have a remote server, "Data Source=server name_or_ip;Initial Catalog=your_database_name;UID=sa;pwd= * " If you have a instance name of your installation of sql server, make your Data Source look like this. "Data Source=.\\mysqlinstance;"

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