private void btnsv_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=tubd_data;Data Source=TIMY\SQLEXPRESS");
try
{
con.Open();
statusStrip1.Text="Connection Succesfull !!!";
SqlCommand cmd = con.CreateCommand();
cmd.CommandText="Insert into must_first [invoice_no], [date], [name], [total], [total_iv] values ('" + invo_no.Text + "','" + bill_datetxt.Text + "','" + txtname.Text + "','" + txttot.Text + "','" + txtvatinclu.Text + "')";
con.Close();
try
{
cmd.ExecuteNonQuery();
statusStrip1.Text="Record Updated!!!";
}
catch(Exception)
{
statusStrip1.Text="Record Update Error!!!";
}
}
catch(Exception)
{
statusStrip1.Text="Connection Fail !!!";
}
}
解决方案
Hello Abhishek,
Try adding an extra ''\'' before SQLEXPRESS. Please refer to a nice article[^] here on CodeProject to learn more String escaping.
Regards,
SqlConnection con = new SqlConnection("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=tubd_data;Data Source=TIMY\SQLEXPRESS");
//Add @ symbol before Provider. It will work fine. @--->Escape characters
//When you are using \ symbol in connection that time you will face problem if you are not using @
SqlConnection con = new SqlConnection(@"Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=tubd_data;Data Source=TIMY\SQLEXPRESS");