简体   繁体   English

C#中的连接字符串

[英]Connection String in c#

I am trying to connect to a database with my connection string and recieve the following error when trying to connect to the database. 我尝试使用连接字符串连接到数据库,并在尝试连接到数据库时收到以下错误。 For intergrated Security I user SSID so I don't have to enter a username and password. 对于集成的安全性,我使用SSID,因此不必输入用户名和密码。 Also, the database resides on the same machine and was created inside VS2010. 此外,数据库位于同一台计算机上,并且是在VS2010内部创建的。 I can connect to the db without a problem using the SqlDataSource, but I am looking to start writing my own connection strings. 我可以使用SqlDataSource毫无问题地连接到db,但是我希望开始编写自己的连接字符串。

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        string source = "server=(local)" + "integrated security=SSPI;" + "Connect     Timeout=30; "  + "database=Name";
        SqlConnection conn = new SqlConnection(source);
        conn.Open();

        conn.Close();

    }

The Error I get is this: 我得到的错误是这样的:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. 建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 The server was not found or was not accessible. 服务器未找到或无法访问。 Verify that the instance name is correct and that SQL Server is configured to allow remote connections. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (提供者:命名管道提供程序,错误:40-无法打开与SQL Server的连接)

You are missing a ; 你想念一个; after the server name. 服务器名称之后。

Why are you concatenating all the parts of the connection string if they do not change? 如果连接字符串的所有部分都没有更改,为什么要串联它们呢? It makes it more difficult to read. 它使阅读更加困难。

Try this: 尝试这个:

string source = "server=(local);integrated security=SSPI;ConnectTimeout=30;database=Name";

Try the following syntax as connection string: 尝试使用以下语法作为连接字符串:

string source = "Data Source=Server Address;Initial Catalog=Database Name;Integrated Security=SSPI;";

Where Server Address should be localhost or .\\SQLExpress 服务器地址应为localhost或。\\ SQLExpress

Hope thats correct. 希望那是正确的。 I have'nt installed a vs for testing 我尚未安装vs进行测试

You need a semicolon after (local) (本地)后需要分号

string source = "server=(local);" + "integrated security=SSPI

I notice you tagged the question with asp.net by default asp.net runs under a system account, when you are using integrated security then that account is trying to access your database, it probably doesn't have permission. 我注意到您默认情况下用asp.net标记了问题,因为asp.net在系统帐户下运行,当您使用集成安全性时,该帐户正试图访问您的数据库,它可能没有权限。

Take a look here for some information. 在这里查看一些信息。

I would recommend you to use the SqlConnectionStringBuilder : http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder(v=VS.100).aspx 我建议您使用SqlConnectionStringBuilderhttp : //msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnectionstringbuilder( SqlConnectionStringBuilder .aspx

That will probably make it easier. 这样可能会更容易。

To connect to SQL Server from C#.NET, you need to create a connection string such as below: 要从C#.NET连接到SQL Server,您需要创建一个如下的连接字符串:

private SqlConnection connection;
private string connectionString =
    @"Server=(local);Database=Embedding_SQL_Test;User ID=sa;Password=123";
connection = new SqlConnection( connectionString );

Next, you use the SqlConnection object created above to create a 'SqlCommand', as shown below: 接下来,使用上面创建的SqlConnection对象创建一个“ SqlCommand”,如下所示:

SqlCommand cmd = new SqlCommand( "select * from Customer where CustomerID = @Cid",
    connection); 

The SQL query shown here can be replaced by a SELECT , INSERT , UPDATE queries etc. 此处显示的SQL查询可以替换为SELECTINSERTUPDATE查询等。

Next to execute the SQL queries in the database, you use the following methods: ExecuteReader - to execute SELECT queries ExecuteNonQuery - to execute INSERT , DELETE , UPDATE , and SET statements. 接下来要在数据库中执行SQL查询,您可以使用以下方法: ExecuteReader执行SELECT查询ExecuteNonQuery执行INSERTDELETEUPDATESET语句。

This is a very short description of how to connect to SQL Server database from C# and execute SQL queries in the database. 这是关于如何从C#连接到SQL Server数据库以及如何在数据库中执行SQL查询的简短描述。 For details about the connection string, the methods and their parameters check the following link: ( http://www.shahriarnk.com/Shahriar-NK-Research-Embedding-SQL-in-C-Sharp-Java.html ) Here you will also find details about how to pass parameters to the SQL queries as well as calling stored procedures and much more. 有关连接字符串,方法及其参数的详细信息,请检查以下链接:( http://www.shahriarnk.com/Shahriar-NK-Research-Embedding-SQL-in-C-Sharp-Java.html )还将找到有关如何将参数传递给SQL查询以及调用存储过程的详细信息。

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

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