开发者

SQL Server, Visual Studio INSERT record connection problem

protected void Button1_Click(object sender, EventArgs e)
{
    SqlConnection badersql = new SqlConnection("server = SQLSERVER; uid = sa; pwd = 123; database = webpage");
    badersql.Open();

    SqlCommand insert_user= new SqlCommand("insert into webpage.dbo.users (username,password,firstname,lastname,address,country,city,phonenumber,gender,email) VALUES (' bader','123','beno','venp','33','pal','d',''1234','male','pal@pal.com')");

    badersql.Close();
}

I am trying to insert a record when I click on button1, but its seems every time I try that a msg pops up saying

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)

My full server name is BADER-VAIO\SQLEXPRESS

My database name is webpage

开发者_如何学JAVAMy table name is "dbo.users" as the server names it on the left pane menu.

I am using the Web Visual Studio 2010 Express and SQL Server 2010 Express

Any suggestions please ?


Its a problem with your SqlConnection. It is not able to connect to your server it may be your connection string. Also, at the same time check if your database server is up and running. Look for the correct syntax of connection strings here.

Should be something like this -

SqlConnection newconn = 
   new SqlConnection("Data Source=BADER-VAIO\SQLEXPRESS;Initial Catalog=webpage;User Id=sa;Password=123");


The error message is usually due to one of two problems:

1- The connection string is incorrect, which in your example is true. Note that the Server=SQLEXPRESS portion of the connection string is missing the server name portion. You are only specifying the Instance name: SQLEXPRESS but are not including the server name: BADER-VAIO.

So, the first thing you need to attempt is change the string to:

"server=BADER-VAIO\SQLSERVER; uid = sa; pwd = 123; database = webpage"

Another alternative is to try:

"Data Source=BADER-VAIO\SQLSERVER;User Id=sa;Password=123;Initial Catalog=webpage"

2- The second reason for this problem is due to security issues. Note that this could be either due to a configuration that might need to be tweaked or to the user you are trying to connect with not being configured properly. Since you are using the sa, I would assume that it could be a server configuration that might need to done.

Well, try the first option and if it doesn't work, we can attempt to figure out what configuration changes need to be done to your SQL Server instance.

UPDATE

Note that for C# you might need to escape the \ character, which can be done either by using @ or using double \ for the server like BADER-VAIO\\SQLSERVER. An example of the first method is:

SqlConnection conn = new SqlConnection(@"server=BADER-VAIO\SQLSERVER; uid = sa; pwd = 123; database = webpage");

Note the @ before the connection string.

Also as an aside, you might want to wrap the SqlConnection in a using so that it automatically disposes the connection once you are done with it. Also it is advised to store the connection string in the web.config file. Lastly, you are not connecting the SqlCommand to the SqlConnection that you created.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜