Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

c# - Sql connection-string for localhost server

I am newbie in this .NET and please don't mind in answering my simple question. I am trying to write a windows application, where in I am using a localhost SQLserver for database.

I need to know what is the exact connection string for my localhost, if my server name looks like as below:

Data Source=HARIHARAN-PCSQLEXPRESS;Initial Catalog=master;Integrated Security=True

should i need to give this same as connection string, or is something wrong in this syntax.

whn i try to open my connection. I am seeing error in opening connection.

How the format of connection string should be? any one please guide me.

I tried like this :

 private void button1_Click(object sender, EventArgs e)
    {
        string str = "Data Source=HARIHARAN-PCSQLEXPRESS;Initial Catalog=master;Integrated Security=True" ; 
        SqlConnection con = new SqlConnection(str);
        SqlCommand cmd = new SqlCommand();
        SqlDataReader r;

        cmd.CommandText = "SELECT * from Table1";
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;

        con.Open();

        r = cmd.ExecuteReader();

        con.Close();


    }

This code errors out at con.Open();

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using the default instance (i.e., MSSQLSERVER, use the DOT (.))

<add name="CONNECTION_STRING_NAME" connectionString="Data Source=.;Initial Catalog=DATABASE_NAME;Integrated Security=True;" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...