private void btnAdd_Click(object sender, EventArgs e)
{
// Add the company information
SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=sql;database=group1");
con.Open(); string strInsert = " INSERT INTO company ( companyName , companyAddr , companytel ) VALUES ( "; strInsert += this.txtName.Text + ", '";
strInsert += this.txtAddr.Text + "', '";
strInsert += this.txtTel.Text + ")"; SqlCommand cmd = new SqlCommand (strInsert, con); try
{
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Add data success !");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (con != null)
{
con.Close();
}
}
}
{
// Add the company information
SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=sql;database=group1");
con.Open(); string strInsert = " INSERT INTO company ( companyName , companyAddr , companytel ) VALUES ( "; strInsert += this.txtName.Text + ", '";
strInsert += this.txtAddr.Text + "', '";
strInsert += this.txtTel.Text + ")"; SqlCommand cmd = new SqlCommand (strInsert, con); try
{
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Add data success !");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (con != null)
{
con.Close();
}
}
}
string strInsert = " INSERT INTO company ( companyName , companyAddr , companytel ) VALUES ( '";
strInsert += this.txtName.Text + "', '";
strInsert += this.txtAddr.Text + "', '";
strInsert += this.txtTel.Text + "')";不过你这样拼串习惯不好,用参数比较好,参看
http://blog.csdn.net/knight94/archive/2006/04/15/664530.aspx