开发者

Connection String in c#

I am trying to connect to a database with my connection string and recieve the followi开发者_如何学编程ng 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. Also, the database resides on the same machine and was created inside VS2010. I can connect to the db without a problem using the SqlDataSource, but I am looking to start writing my own connection strings.

    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. 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. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)


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

Hope thats correct. I have'nt installed a vs for testing


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";


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.

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

That will probably make it easier.


To connect to SQL Server from C#.NET, you need to create a connection string such as below:

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:

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.

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.

This is a very short description of how to connect to SQL Server database from C# and execute SQL queries in the database. For details about the connection string, the methods and their parameters check the following link: ( http://www.shahriarnk.com/Shahriar-N-K-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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜