private void button1_Click(object sender, EventArgs e)
{
string sql = “”;
switch (this.Flag)
{
case 1:
sql = “select * from studentTable where stuId=”” + textBox1.Text + “””;
break;
case 2:
sql = “select * from studentTable where stuId=”” + textBox1.Text + “””;
break;
case 3:
sql = “delete from studentTable where stuId=”” + textBox1.Text + “””;
break;
case 4:
sql=”insert into studentTable(stuId,stuName,stuSex,stuAge,stuCity,stuTele) values(“”
+textBox1.Text+””,””+textBox2.Text+””,””+textBox3.Text+””,””+textBox4.Text+””,””
+textBox5.Text+””,””+textBox6.Text+””)”;
break;
default:
break;
}
SqlConnection lo_conn = new SqlConnection(“server=(local)\SQLEXPRESS;database=studentData;user id=sa;password=12547”);
lo_conn.Open();
SqlCommand lo_cmd = new SqlCommand();
lo_cmd.CommandText = sql;
lo_cmd.Connection = lo_conn;
try
{
SqlDataReader lo_Read = lo_cmd.ExecuteReader();
if (this.flag == 1 || this.flag==2)
{
if(lo_Read.Read())
{
textBox2.Text =Convert.ToString( lo_Read[1]);
textBox3.Text = lo_Read[2].ToString();
textBox4.Text = lo_Read[3].ToString();
textBox5.Text = lo_Read[4].ToString();
textBox6.Text = lo_Read[5].ToString();
}
}
{
string sql = “”;
switch (this.Flag)
{
case 1:
sql = “select * from studentTable where stuId=”” + textBox1.Text + “””;
break;
case 2:
sql = “select * from studentTable where stuId=”” + textBox1.Text + “””;
break;
case 3:
sql = “delete from studentTable where stuId=”” + textBox1.Text + “””;
break;
case 4:
sql=”insert into studentTable(stuId,stuName,stuSex,stuAge,stuCity,stuTele) values(“”
+textBox1.Text+””,””+textBox2.Text+””,””+textBox3.Text+””,””+textBox4.Text+””,””
+textBox5.Text+””,””+textBox6.Text+””)”;
break;
default:
break;
}
SqlConnection lo_conn = new SqlConnection(“server=(local)\SQLEXPRESS;database=studentData;user id=sa;password=12547”);
lo_conn.Open();
SqlCommand lo_cmd = new SqlCommand();
lo_cmd.CommandText = sql;
lo_cmd.Connection = lo_conn;
try
{
SqlDataReader lo_Read = lo_cmd.ExecuteReader();
if (this.flag == 1 || this.flag==2)
{
if(lo_Read.Read())
{
textBox2.Text =Convert.ToString( lo_Read[1]);
textBox3.Text = lo_Read[2].ToString();
textBox4.Text = lo_Read[3].ToString();
textBox5.Text = lo_Read[4].ToString();
textBox6.Text = lo_Read[5].ToString();
}
}
MessageBox.Show(“数据库操作成功”, “sql Server”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
{
MessageBox.Show(“sql命令执行失败”, “sql Server”, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
解决方案
20
请检查:
1、lo_conn是不是连接成功
2、textBox1.Text 有没有值
3、表名studentTable是不是正确,数据库里,有没有 stuId==textBox1.Text 的数据
4、 if (this.flag == 1 || this.flag==2)是不是可以进去if代码块,flag到底是多少
1、lo_conn是不是连接成功
2、textBox1.Text 有没有值
3、表名studentTable是不是正确,数据库里,有没有 stuId==textBox1.Text 的数据
4、 if (this.flag == 1 || this.flag==2)是不是可以进去if代码块,flag到底是多少
20
调试你的sql,你的sql条件不满足,无法查询到数据