how to insert values to the database using C# in Windows Application

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rekhasc
    New Member
    • Oct 2007
    • 66

    how to insert values to the database using C# in Windows Application

    i am using the database SQLSERver 2005, i m new to this i worked in sql server2000... but using sqlserver 2005 i dont know how to insert the values...
    i write the code but the values is not storing in the database if anybody knows plz help me...
    sql server2005
    server type: database Engine
    authentication :windows authentication

    here is my code

    private void butinsert_Click (object sender, EventArgs e)
    {
    SqlConnection con=new SqlConnection(" server=localhos t;user id=sa;password= ;database=D:\\c ompany");
    SqlCommand cmd=new SqlCommand("ins ert into emp(name,age,sa lary) values('"+txtna me.Text+"','"+t xtage.Text+"',' "+txtsalary.Tex t+"')",con);

    con.Open();
    int i;
    i=cmd.ExecuteNo nQuery();
    if(i>0)
    {
    Console.WriteLi ne("inserted Successfully");
    }


    con.Close();
    }

    it showing this errors:::
    An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
    Last edited by rekhasc; May 14 '08, 09:02 PM. Reason: to tell ahout the error
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    Your connection string might be incorrect.

    Have a look at connectionstrin gs.org and find the connection string which suits you

    Comment

    Working...