如何定义要在 VB.NET 中使用的 Sql Server 连接字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16126468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 14:55:23  来源:igfitidea点击:

How to define a Sql Server connection string to use in VB.NET?

sqlvb.netvisual-studio-2012

提问by Phil J Fry

How to: connectionString?

如何:connectionString

Hopefully a simple question, but I am too new to Visual Basicto understand : http://msdn.microsoft.com/en-us/library/d7469at0.aspx...

希望是一个简单的问题,但我太新而Visual Basic无法理解:http: //msdn.microsoft.com/en-us/library/d7469at0.aspx...

I am writing an app in VBand trying to connect it to a "Local Database" / "Dataset".

我正在编写一个应用程序VB并尝试将其连接到“本地数据库”/“数据集”。

I received help earlier today to get the code listed below and it appears it will work just fine, except I have no idea how to connect my application to my dataset. From the reading I have been doing, it seems the connectionString would be to connect it to a database that was created with SQL Server. ???

我今天早些时候收到了帮助以获取下面列出的代码,它似乎可以正常工作,但我不知道如何将我的应用程序连接到我的数据集。从我一直在做的阅读来看,connectionString 似乎是将它连接到使用 SQL Server 创建的数据库。???

On my VB Windows Form Application, I simply did "Add New Item" then "Local Database" and it asked me what type of database model and I selected "Dataset".

在我的 VB Windows 窗体应用程序上,我只是先“添加新项目”然后“本地数据库”,它问我什么类型的数据库模型,我选择了“数据集”。

I only have two tables and just need to be able to connect to them. Here is the code or at least the idea of what I would like to do. Please help

我只有两张桌子,只需要能够连接到它们。这是代码或至少是我想做的事情的想法。请帮忙

    Using sqlCon = New SqlConnection(connectionString)
        sqlCon.Open()
        Dim sqlText = "UPDATE appTable SET clickCount + 1 " & _
            "WHERE appName = @name"
        Dim cmd = New SqlCommand(sqlText, sqlCon)
        cmd.Parameters.AddWithValue("@name", appName)
        cmd.ExecuteNonQuery()
    End Using

I am so sorry for this poorly phrased question, I have been reading and trying to learn this for too long and have frustrated myself. I come from Python with a lot less syntax and "rules" and I feel I could be way farther on this project and to be stuck on simply connecting to the "database" has me beyond frustrated :/

对于这个措辞不当的问题,我感到非常抱歉,我一直在阅读并试图学习这个太久了,但我自己也感到很沮丧。我来自 Python 的语法和“规则”要少得多,我觉得我可以在这个项目上走得更远,并且只停留在简单地连接到“数据库”上,这让我感到非常沮丧:/

Thanks for assistance in advance.

提前感谢您的帮助。

answer : There is an App.config file in my Solution Explorer with the connectionString Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\gadgetDatabase.mdf;Integrated Security=True

答案:我的解决方案资源管理器中有一个 App.config 文件,其中 connectionString Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\gadgetDatabase.mdf;Integrated Security=True

回答by Preet Sangha

Try

尝试

Dim connectionString AS String = "Server=my_server;Database=name_of_db;User Id=user_name;Password=my_password"

And replace my_server, name_of_db, user_name and my_password with your values.

并用您的值替换 my_server、name_of_db、user_name 和 my_password。

then Using sqlCon = New SqlConnection(connectionString)should work

然后Using sqlCon = New SqlConnection(connectionString)应该工作

also I think your SQL is wrong, it should be SET clickCount= clickCount + 1I think.

我也认为你的 SQL 是错误的,应该是SET clickCount= clickCount + 1我认为的。

And on a general note, the page you link to has a link called Connection Stringwhich shows you how to do this.

一般而言,您链接到的页面有一个名为Connection String的链接,它向您展示了如何执行此操作。

回答by SMHasnain

Standard Security:

标准安全:

Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;

Trusted Connection:

可信连接:

Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

will be glad if it helps.

如果有帮助会很高兴。

Regards.

问候。

回答by Linus Caldwell

Set the connection string in your config file:

在配置文件中设置连接字符串:

<connectionStrings>
    <add name="ConnString"
         connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\gadgetDatabase.mdf;Integrated Security=True" />
</connectionStrings>

回答by Gábor Boros

May it will help for u. U should use (localdb).

愿它对你有帮助。你应该使用(localdb)。

LocalDB automatic instance

LocalDB 自动实例

Server=(localdb)\v11.0;Integrated Security=true;

Server=(localdb)\v11.0;Integrated Security=true;

LocalDB automatic instance with specific data file

具有特定数据文件的 LocalDB 自动实例

Server=(localdb)\v11.0;Integrated Security=true; AttachDbFileName=C:\MyFolder\MyData.mdf;

Server=(localdb)\v11.0;Integrated Security=true; AttachDbFileName=C:\MyFolder\MyData.mdf;

回答by clarifier

The Connection String Which We Are Assigning from server side will be same as that From Web config File. The Catalog: Means To Database it is followed by Username and Password And DataClient The New sql connection establishes The connection to sql server by using the credentials in the connection string.. Then it is followed by sql command which retrives the required data in the dataset and then we assing them to required variables or controls to get the required task done

我们从服务器端分配的连接字符串将与来自 Web 配置文件的连接字符串相同。目录:指向数据库,后面是用户名和密码以及 DataClient 新的 sql 连接通过使用连接字符串中的凭据建立到 sql server 的连接。然后是 sql 命令,它检索数据集中所需的数据然后我们将它们分配给所需的变量或控件以完成所需的任务

回答by Awais Siddique

Use the following Imports

使用以下 Imports

Imports System.Data.SqlClient
Imports System.Data.Sql

Public SQLConn As New SqlConnection With {.ConnectionString = "Server=Desktop1[enter image description here][1];Database=Infostudio; Trusted_Connection=true;"}

Full string: enter image description here

完整字符串: 在此处输入图片说明

回答by Hazrat Ali

Imports System.Data.SqlClient
Imports System.Data.Sql
Imports System.IO
Imports System.Configuration
Dim connectionString As String = ConfigurationManager.ConnectionStrings("ConString").ConnectionString
Dim cn As New SqlConnection(connectionString)
Dim cmd As New SqlCommand
Dim dr As SqlDataAdapter