c# 中进行数据库操作出错,请问哪里有误??
private void btnAdd_Click(object sender, EventArgs e)//有误{
if (sqlCon.State == ConnectionState.Open)
sqlCon.Close();
sqlCon.Open();
if (this.txtSid.Text == "")
{
MessageBox.Show("请输入学号");
this.txtSid.Focus();
return;
}
= "select count(*) from student where Sid='" + this.txtSid.Text + "'";
sqlCmd.Connection = sqlCon;
int i = Convert.ToInt32(sqlCmd.ExecuteScalar());
if (i > 0)
{
MessageBox.Show("该学号已经存在,请重新输入!");
this.txtSid.SelectionStart = 0;
this.txtSid.SelectionLength = this.txtSid.Text.Length;
this.txtSid.Focus();
return;
}//判断学号
if (this.txtSname.Text == "")
{
MessageBox.Show("请输入姓名!");
this.txtSname.Focus();
return;
}//判断姓名
if (this.txtSphone.Text != "")
{
if (this.IsNumber(this.txtSphone.Text) == false)
{
MessageBox.Show("电话号码应该输入数字!");
this.txtSphone.SelectionStart = 0;
this.txtSphone.SelectionLength = this.txtSphone.Text.Length;
this.txtSphone.Focus();
return;
}
}//判断电话号码
string sfex = "";
sqlCmd.Connection = sqlCon;
if (this.rdbMan.Checked == true)
sfex = "男";
else
sfex = "女";
string strAdd = "insert into student(Sname,Sfex,Sid,Sphonenumber,Sbirthday)values('" + this.txtSname.Text + "','" + sfex + "','" + this.txtSid.Text + "','" + this.txtSphone.Text + "','" + this.txtSbirthday.Text + "')";
= strAdd;//哪里错了???
sqlCmd.ExecuteNonQuery();
MessageBox.Show("成功!");
/* = "insert into student(Sname,Sfex,Sid,Sphonenumber,Sbirthday)values('" + this.txtSname.Text + "','" + sfex + "','" + this.txtSid.Text + "','" + this.txtSphone.Text + "','" + this.txtSbirthday.Text + "')";
sqlCmd.ExecuteNonQuery();
MessageBox.Show("学生记录添加成功!");
* */
/*
* = "insert into stu values('" + this.txtID.Text + "','" + this.txtName.Text + "','" + stuSex + "','" + dtpBirth.Text + "','" + this.txtPhone.Text + "')";
sqlCmd.ExecuteNonQuery();
// MessageBox.Show("学生记录添加成功!");
* */
this.clear();
this.fillGrid();
}
public bool IsNumber(string str){
try{
long var1 = Convert.ToInt64(str);
return true;
}
catch{
return false;
}
}//判断是否是数字
public void fillGrid(){
this.listView1.Items.Clear();
if(sqlCon.State==ConnectionState.Open)
sqlCon.Close();
sqlCon.Open();
sqlCmd.Connection=sqlCon;
* from student";
SqlDataReader sdr=sqlCmd.ExecuteReader();
while(sdr.Read())
{
ListViewItem lv=new ListViewItem(sdr[3].ToString());
lv.SubItems.Add(sdr[0].ToString());
lv.SubItems.Add(sdr[2].ToString());
lv.SubItems.Add(sdr[4].ToString());
lv.SubItems.Add(sdr[5].ToString());
listView1.Items.Add(lv);
//添加一行
}
}
//添加
程序运行时说
string strAdd = "insert into student(Sname,Sfex,Sid,Sphonenumber,Sbirthday)values('" + this.txtSname.Text + "','" + sfex + "','" + this.txtSid.Text + "','" + this.txtSphone.Text + "','" + this.txtSbirthday.Text + "')";
= strAdd;//哪里错了???
sqlCmd.ExecuteNonQuery();
MessageBox.Show("成功!");
有错,可是不知道哪里错了,可以帮忙解答一下吗??
数据库如下:
create database a;
create table student
(
Sname varchar(10),
Spwd varchar(20),
Sfex varchar(4),
Sid varchar(20),
Sphonenumber varchar(15),
Sbirthday varchar(20)
)
select * from student
insert into student values('huangli','123','女','1225112009','18850571140','1994-11-02')
drop table student