简体   繁体   English

VB.net连接到数据库

[英]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. 我正在使用Visual Studio 2010编写一些VB脚本,该脚本读取一些txt文件并将结果输入到数据库中。 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. 我连接的数据库也是Microsoft SQL Server 2008 Service Pack2。我试图使用指定数据源,数据库,用户ID和密码的连接字符串。 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/ 您可以在此处获取有关连接字符串的信息: 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. 另外,请检查服务器字符串中服务器网络名称与IP地址或localhost与127.0.0.1的使用关系。

If you have your SQL server on the same machine as Visual Studio. 如果您的SQL Server与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. “初始目录=您的数据库名称”-将在您的SQL Server中使用此名称的数据库。 "Integrated Security=True" - will use your own user to connect to the database. “集成安全性= True”-将使用您自己的用户连接到数据库。 This requires that you have access to the sql server. 这要求您有权访问sql服务器。 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. 如果您有远程服务器,请尝试此操作,“数据源=服务器名或IP地址;初始目录=您的数据库名; UID = sa; pwd = * “如果您具有安装的sql服务器的实例名称,则使数据源看起来像这样。 "Data Source=.\\mysqlinstance;" “数据源=。\\ mysqlinstance;”

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

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