求教:往数据库里添加数据的问题!!!!
往数据库里添加数据的问题!
我的代码如下:
SqlConnection thisConnection;
thisConnection = new SqlConnection( "DataSource=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\data\\student
_Data.MDF;Integrated Security=True;Connect Timeout=30;UserInstance=True ");
thisConnection.Open();
SqlCommand cmd = new SqlCommand( "select * from stu where SID= ' " + txtSId.Text.Trim() + " ' ", thisConnection);
if (cmd.ExecuteScalar() != null)
{
MessageBox.Show( "输入的学号有重复 ", "提示 ", 0,
MessageBoxIcon.Warning);
}
else
{
string strsql1 = "insert into stu(SId,Sname,identitynum,class,sex,birth,nation,politistate,foreigner,
recruit) values ( ' " + txtSId.Text.Trim() + " ', ' " + txtSname.Text.Trim() + " ', ' " + txtidentity.Text.Trim() + " ', ' " + txtclass.Text.Trim() + " ', ' " + txtsex.Text.Trim() + " ', ' " + txtbirth.Text.Trim() + " ', ' " + txtnation.Text.Trim() + " ', ' " + txtpolitics.Text.Trim() + " ', ' " + txtforeign.Text.Trim() + " ', ' " + txtrecruit.Text.Trim() + " '); ";
cmd.CommandText = strsql1;
cmd.ExecuteNonQuery();
MessageBox.Show( "专业信息添加成功 ", "提示 ",0,MessageBoxIcon.Information);
}
this.stuTableAdapter.Fill(this.student_DataDataSet.stu);