//学生表 新增
private void btnSadd_Click(object sender, System.EventArgs e)
{
if (txtSno.Text.Length==0)
{
MessageBox.Show("请输入学号!!");
return;
};
try
{
string strSno=txtSno.Text;
if (FindSno(strSno))//按学号查找一个学生龙活虎
{
MessageBox.Show("学生:"+strSno+"已经存在!!\n 无法新增一个学生信息");
}
else//没有找到
{
//新增的操作
string strCommSQL="Data Source=localhost;User ID=szu;password=szu;Initial Catalog=szustudent";
sConn=new SqlConnection(strCommSQL);
sConn.Open();
//打开连接,执行Command,建立reader;
//string strSno=txtSno.Text;
string strSname=txtSname.Text;
string strSsex=txtSsex.Text;
string strSbirth=txtSbirth.Text;
string strSclass=txtSclass.Text;
string strSmajor=txtSmajor.Text;
string strScolle=txtScolle.Text;
string strSaddi=txtSaddi.Text; string strAddOne="INSERT INTO student VALUES( Sno=001);";
SqlCommand AddOne=new SqlCommand(strAddOne,sConn);
AddOne.ExecuteNonQuery(); string strCommAdd="INSERT INTO student VALUES("
+"strSno"+","+"strSname"+","+"strSsex"+","+"strSbirth"+","+"strSclass"
+","+"strSmajor"+","+"strScolle"+","+"strSaddi"+");";
SqlCommand CommAdd=new SqlCommand(strCommAdd,sConn);
CommAdd.ExecuteNonQuery();
MessageBox.Show("新增了一个学生!"); }
}
catch
{
MessageBox.Show("程序异常!");
}
}
程序执行到“CommAdd.ExecuteNonQuery();”产生异常,
怎么办?
多谢了!
private void btnSadd_Click(object sender, System.EventArgs e)
{
if (txtSno.Text.Length==0)
{
MessageBox.Show("请输入学号!!");
return;
};
try
{
string strSno=txtSno.Text;
if (FindSno(strSno))//按学号查找一个学生龙活虎
{
MessageBox.Show("学生:"+strSno+"已经存在!!\n 无法新增一个学生信息");
}
else//没有找到
{
//新增的操作
string strCommSQL="Data Source=localhost;User ID=szu;password=szu;Initial Catalog=szustudent";
sConn=new SqlConnection(strCommSQL);
sConn.Open();
//打开连接,执行Command,建立reader;
//string strSno=txtSno.Text;
string strSname=txtSname.Text;
string strSsex=txtSsex.Text;
string strSbirth=txtSbirth.Text;
string strSclass=txtSclass.Text;
string strSmajor=txtSmajor.Text;
string strScolle=txtScolle.Text;
string strSaddi=txtSaddi.Text; string strAddOne="INSERT INTO student VALUES( Sno=001);";
SqlCommand AddOne=new SqlCommand(strAddOne,sConn);
AddOne.ExecuteNonQuery(); string strCommAdd="INSERT INTO student VALUES("
+"strSno"+","+"strSname"+","+"strSsex"+","+"strSbirth"+","+"strSclass"
+","+"strSmajor"+","+"strScolle"+","+"strSaddi"+");";
SqlCommand CommAdd=new SqlCommand(strCommAdd,sConn);
CommAdd.ExecuteNonQuery();
MessageBox.Show("新增了一个学生!"); }
}
catch
{
MessageBox.Show("程序异常!");
}
}
程序执行到“CommAdd.ExecuteNonQuery();”产生异常,
怎么办?
多谢了!
解决方案 »
- 如何判断数据库连接超时
- 大家好 我又来求助了 关键字:c# emgu(opencv)
- 请问关于组件性能问题
- 请教关于Properties.Settings中的问题
- c++中的指针在c#中怎么表示的好
- 我那这样的程序去应聘小公司,如果你是老板你会要我吗?注意时小公司。
- 如何得到事件关联的方法是什么?
- 怎么使用工商银行提供的网银接口
- 怎么获取系统时间,并传给dateTimePicker1控件
- 有没有Inforpower的C#版的控件啊,或类似的,那套控件实在是太棒了
- 我想在DATABRID中的每一列中都加以个BUTTON,可总是不成功!!
- 我知道给你0分你会帮我的!关于Web Application程序的访问的问题
这句有问题吧
insert 语句不是这样写的呀
你后面的那句就写对的~~
或用Sql事件探测器跟踪一下。
前面打开,这样出了问题连接不是总要开着吗?