开发者

sql connection problem with asp.net

i m using vs2010 and i have tried all the possible ways nearest to me to connect with the database from visualstudio plz help me heres the code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection cs = new SqlConnection("Data Source=Sql Server(SQLEXPRESS); Initial Catalog=school");
      开发者_JS百科      cs.Open();
                MessageBox.Show(cs.State.ToString());
                cs.Close();
        }
    }
}

it doesnt connet and give error: 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)

help


Hey i have done it its very simple i research for escape sequence and the solution is instead of one backslash we should place 2 backslash lol maybe this would aswell help others and is necessary if u r running express

SqlConnection cs = new SqlConnection("Data Source=Your PC Name\\SQLEXPRESS; Initial Catalog=school;Integrated Security=true")

Remember to put 2 backslash after ur PC name :) :)

thanks for ur answers pals


Your connectionstring is wrong. Have a look at http://www.connectionstrings.com. Also, if you just installed an SQL server (or SQL Express), make sure you have the server set up for connections over IP. You can check that in the SQL Server Configuration Manager:

Open the SQL Server Config Manager and select SQL Native Client Configuration > Client Protocols from the tree. In the window on the right side, make sure TCP/IP is enabled.

Check the same thing for SQL Server Network Configurations > Protocols for SQLEXPRESS


Try connecting this way:

SqlConnection cs = new SqlConnection("Data Source=.\SQLEXPRESS; Initial Catalog=school;Integrated Security=true")

I assume SQL Server is installed on your pc, if not then try:

SqlConnection cs = new SqlConnection("Data Source=SomeIpAddress\SQLEXPRESS; Initial Catalog=school;Integrated Security=true")
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜