简体   繁体   中英

How can I connect my application to a SQL Server that is on another computer?

I have created a Winforms app to insert data into database.That works fine with my local Database.mdf . But I need to connect my application to SQL Server 2014 that is installed on other computer/domain.

My logon screen for SQL Server looks like this:

登录屏幕SQL Server 2014

My code for insert data into database is:

 private void btnPrint_Click(object sender, EventArgs e)
 {
    string connetionString = null;

    //connetionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\NIKOPOL\Desktop\Barcode ver1\Barcode ver1\Database2.mdf";
    connectionString = @"Data Source=10.89.149.12,1433;Network Library=DBMSSOCN;Initial Catalog=KontrolniCOC; User ID=Boris;Password=entermypassword";
    SqlConnection cnn = new SqlConnection(connectionString);

    try
    {
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = cnn;

        cmd.CommandText = "INSERT into...";
        cnn.Open();
        cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    finally
    {
        cnn.Close();
    }

So I guess that all I need is that I change my connection string to:

 connetionString = @"Data Source=10.89.149.12,1433;Network Library=DBMSSOCN;Initial Catalog=KontrolniCOC; User ID=Boris;Password=entermypassword";

But it throws an exception 0x80131904 a network-related...

在此处输入图片说明

My app will be run on a computer that has no SQL Server installed.

[enter image description here][1]

[1]: https://i.stack.imgur.com/OhaVt.png ** hope this helps, I have edited the active code in the office for you.**

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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