user1918566
user1918566

Reputation: 221

How to establish database(SQL) Connection using c#?

I want to establish a database connection for receiving the details of the customers using C#. I am using sql-server 2008 along with visual studio 2010.....

Here is this code:

<asp:Textbox id="TxtBox1" runat="server" />
<asp:Textbox id="TxtBox2" runat="server" />
<asp:Button id="Button1" runat="server" Onclick="Button1_Click" />

and the codebehind:

protected void Button1_Click(Object sender,EventArgs e)
{
string Text1=TextBox1.Text;
string Text2=TextBox2.Text;
string connectionString="Data Source=servername;InitialCatalog=DataBaseNameUserID=sa;  Password=YourPassword;"

SqlConnection sqlConnection=new SqlConnection(connectionString);

string insertStatement="INSERT INTO TableName(column1,column2) VALUES Txtb1+","+Txtb2";

SqlCommand sqlCommand=new SqlCommand(insertStatement,sqlConnection);

try
{
sqlConnection.Open();
sqlCommand.ExecuteNonQuery();

}
finally
{
sqlConnection.Close();
}


}

I have created the data.mdf file in it i have created table containing fields but i am getting Connection has not established error can any one help me to resolve my problem???

Upvotes: 0

Views: 49572

Answers (4)

Rashedul.Rubel
Rashedul.Rubel

Reputation: 3584

use the connection string as follows:

connectionString="Data Source=ServerAddress;Initial Catalog=DataBaseName;Integrated Security=SSPI;User ID=Domain\Username;Password=Password;"

Upvotes: 0

Ulises
Ulises

Reputation: 13419

The connection string itself may be the issue, windows has an easy way to create a valid connection string that you can test and then just copy it to your app. I wrote about it a couple of months ago here:

Tips for your app:

  • As pointed by asawyer, don't use the sa account, create a login with access to the database(s) you need.

  • Use the using statement

example:

using (SqlConnection connection = new SqlConnection(connectionString))  
    {  
      //here you open and execute the command
    }

Upvotes: 0

Steve
Steve

Reputation: 216243

This connection string is wrong.

string connectionString="Data Source=servername;InitialCatalog=DataBaseNameUserID=sa;" + 
                        "Password=YourPassword;"

should be

string connectionString="Data Source=servername;InitialCatalog=DataBaseName; UserID=sa;" + 
                        "Password=YourPassword;"

However, NEVER use the sa user in your applications. Create a new user with Management Studio or better use Integrated Security.

Also the remainder of your code should be written in this way

using(SqlConnection sqlConnection=new SqlConnection(connectionString))
{
     string insertStatement="INSERT INTO TableName(column1,column2) VALUES (@col1, @col2)";
     SqlCommand sqlCommand=new SqlCommand(insertStatement,sqlConnection);
     sqlCommand.Parameters.AddWithValue("@col1", Text1);
     sqlCommand.Parameters.AddWithValue("@col2", Text2);
     sqlConnection.Open();
     sqlCommand.ExecuteNonQuery();
}

Upvotes: 5

Slippery Pete
Slippery Pete

Reputation: 3110

This code does not compile. You need to add a semicolon to this line:

string connectionString="Data Source=servername;InitialCatalog=DataBaseNameUserID=sa;  Password=YourPassword;"

Like so:

string connectionString="Data Source=servername;InitialCatalog=DataBaseNameUserID=sa;  Password=YourPassword;";

You also need to change this line:

string insertStatement="INSERT INTO TableName(column1,column2) VALUES Txtb1+","+Txtb2";

To this:

string insertStatement = "INSERT INTO TableName(column1,column2) VALUES " + Text1 + ", " + Text2;

Upvotes: 0

Related Questions