0

I am new at this but I have a simple table in my SQL Server with one column name and 2 rows in it. I am trying to show up the names when pressing a button, I need to know how to connect the SQL Server database to the C# code and how to read from a specific table.

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

        private void button1_Click(object sender, EventArgs e)
        {
            string connetionString;
            SqlConnection cnn;
            connetionString = @"Data Source=DESKTOP-REB699D\SQLEXPRESS;Initial Catalog=DBdemo";
            cnn = new SqlConnection(connetionString);
            cnn.Open();
            MessageBox.Show("Connection Open  !");
            cnn.Close();
        }
    }
}

But that didn't work, it crashes at the cnn.open(); line of code.

8
  • 2
    it crashes Always share the error message with us. Commented Oct 6, 2022 at 15:12
  • What @LarsTech says and then some guessing. You are trying a remote connection? Initially MSSQL Express only allows local access (TCP\IP is disabled). Commented Oct 6, 2022 at 15:15
  • System.Data.SqlClient.SqlException: 'A connection was successfully established with the server, but then an error occurred during the login process. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.)' Commented Oct 6, 2022 at 15:15
  • Use the edit link under your question to include that information. Commented Oct 6, 2022 at 15:16
  • 3
    Your connection string does not contain any information related to authentication like User Id=myUsername;Password=myPassword; or Trusted_Connection=True; or Integrated Security=true;. See SQL Server connection strings Commented Oct 6, 2022 at 15:26

1 Answer 1

3

If you are not using any credentials, its windows authentication.

Does adding Integrated Security=True to the connection string help?

Can you login to the DB without credentials on SSMS using Windows authentication option? If so, you don't need user credentials for connecting to DB. If you require a Username and password there,

  1. you either need to include that in the connection string or
  2. you need to enable SQL Server and Windows Authentication option under security tab to access the database without credentials
Sign up to request clarification or add additional context in comments.

1 Comment

i tried integrated security and it worked ,thank u so much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.