C#在没有任何数据时进行无效的读取尝试,求助

江柒七 2018-05-26 03:18:01
做了一个宿舍管理系统,换宿的一个功能,一直显示没有任何数据时进行无效的读取尝试,不知道哪错了,请大神看到指点一下,感谢感谢
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = Link.Getconn();
string sqlSNO = "select * from INInfo2 where Sno='" + textBox1.Text.Trim() + "'";
SqlCommand cmdSNO = new SqlCommand(sqlSNO, conn);
//SqlDataAdapter da1 = new SqlDataAdapter(cmdSNO);
SqlDataReader dr = cmdSNO.ExecuteReader(CommandBehavior.CloseConnection);
dr.Read();
string Dno = (string)dr["Dno"];
string sqld = "select Now from DInfo where Dno='" + textBox1.Text.Trim() + "'";
SqlCommand cmdd = new SqlCommand(sqld, conn);
SqlDataReader myreader = cmdd.ExecuteReader(CommandBehavior.CloseConnection);
int now = (int)myreader["Now"];
if (dr.Read())
{
string udINInfo = "updata INInfo2 set Sname='" + textBox2.Text + "',Grade='" + textBox3.Text + "',College='" + textBox4.Text + "',Major='" + textBox5.Text + "',Dno='" + textBox6.Text + "',INtime='" + textBox7.Text + "' where Sno='" + textBox1.Text + "'";
SqlCommand cmdud = new SqlCommand(udINInfo,conn);
cmdud.ExecuteNonQuery();
string insert = "insert into CHANGEInfo Sname='" + textBox2.Text + "',Grade='" + textBox3.Text + "',College='" + textBox4.Text + "',Major='" + textBox5.Text + "',Dno='" + textBox6.Text + "',INtime='" + textBox7.Text + "' where Sno='" + textBox1.Text + "'";
SqlCommand cmdin = new SqlCommand(insert, conn);
cmdin.ExecuteNonQuery();
int newNow1 = now - 1;
int newNow2 = now + 1;
string DInfo1 = "updata DInfo set Max='6',Now='"+newNow1+"' where Dno='"+Dno+"'";
string DInfo2 = "updata DInfo set Max='6',Now='" + newNow2 + "' where Dno='" + textBox6.Text + "'";
SqlCommand cmdd1 = new SqlCommand(DInfo1,conn);
SqlCommand cmdd2 = new SqlCommand(DInfo2,conn);
cmdd1.ExecuteNonQuery();
cmdd2.ExecuteNonQuery();
MessageBox.Show("换宿成功!","提示");
}
else
{
MessageBox.Show("对不起,您输入的学生信息未录入系统!请确认学生信息!","提示");
}
}
...全文
1356 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
另外,学号建议改成弹框选取,而不要使用这种直接录入的方式
  • 打赏
  • 举报
回复
引用 12 楼 weixin_40349487 的回复:
你在页面上填写的学号是?自己先判断一下是否属于我说的那四种情况。
江柒七 2018-05-28
  • 打赏
  • 举报
回复
[quote=引用 13 楼 lyq8376 的回复:] 应该属于第四种,NOW为0,学号的意见我采纳试一下的感谢您
江柒七 2018-05-27
  • 打赏
  • 举报
回复
回复 从事CSharp程序开发 ,这个是我换宿的页面,
, 这个是我的表INInfo2,一个学生信息表
,这个是我的表DInfo,一个宿舍信息表
,这个是我的表CHANGEInfo,一个换宿的学生表
我想的是页面填完信息,我查询学生表的信息,有这个学生,宿舍信息表中原有学生的宿舍减1,换的宿舍加一,换宿成功后再换宿学生表中添加信息

  • 打赏
  • 举报
回复
人家在最外边增加了一个
if(dr.Read())
{
    ...............
}
判断,你应该自己理解程序逻辑,并且写出你的逻辑。
  • 打赏
  • 举报
回复
引用 5 楼 weixin_40349487 的回复:
谢谢 从事CSharp程序开发 的回复,试过了您的建议,虽然不报之前的错呢,但是运行之后按了BUTTON就没反应呢,也没报错
你应该学会调试,然后用调试的方式在 csdn 上贴出你的调试画面和问题。 既然争取地跳过了“没有任何数据”的查询结果,这当然就不需要有什么反应啦。
  • 打赏
  • 举报
回复
Sno在INInfo2表里不存在 还是 INInfo2表里对应Sno的Dno为空 还是 Dno在DInfo表里不存在 还是 DInfo表里对应Dno的Now为空?
江柒七 2018-05-27
  • 打赏
  • 举报
回复
试过了您的方法,真的很感谢,有些成效呢,但是我输入的是正确学生信息,它就会一直提示"对不起,您输入的学生信息未录入系统!请确认学生信息!"
  • 打赏
  • 举报
回复
像这种只取一行一列的,不需要使用ExecuteReader 可以改为

            SqlConnection conn = Link.Getconn();
            string sqlSNO = "select Dno from INInfo2 where Sno='" + textBox1.Text.Trim() + "'";
            SqlCommand cmdSNO = new SqlCommand(sqlSNO, conn);
            //SqlDataAdapter da1 = new SqlDataAdapter(cmdSNO);
            object noObj = cmdSNO.ExecuteScalar();
            if (noObj != null && !(noObj is DBNull))
            {
                string Dno = noObj.ToString();
                string sqld = "select Now from DInfo where Dno='" + textBox1.Text.Trim() + "'";
                SqlCommand cmdd = new SqlCommand(sqld, conn);
                object nowObj = cmdd.ExecuteScalar();
                if (nowObj != null && !(nowObj is DBNull))
                {
                    int now = Convert.ToInt32(nowObj);
                    string udINInfo = "updata INInfo2 set Sname='" + textBox2.Text + "',Grade='" + textBox3.Text + "',College='" + textBox4.Text + "',Major='" + textBox5.Text + "',Dno='" + textBox6.Text + "',INtime='" + textBox7.Text + "' where Sno='" + textBox1.Text + "'";
                    SqlCommand cmdud = new SqlCommand(udINInfo, conn);
                    cmdud.ExecuteNonQuery();
                    string insert = "insert into CHANGEInfo Sname='" + textBox2.Text + "',Grade='" + textBox3.Text + "',College='" + textBox4.Text + "',Major='" + textBox5.Text + "',Dno='" + textBox6.Text + "',INtime='" + textBox7.Text + "' where Sno='" + textBox1.Text + "'";
                    SqlCommand cmdin = new SqlCommand(insert, conn);
                    cmdin.ExecuteNonQuery();
                    int newNow1 = now - 1;
                    int newNow2 = now + 1;
                    string DInfo1 = "updata DInfo set Max='6',Now='" + newNow1 + "' where Dno='" + Dno + "'";
                    string DInfo2 = "updata DInfo set Max='6',Now='" + newNow2 + "' where Dno='" + textBox6.Text + "'";
                    SqlCommand cmdd1 = new SqlCommand(DInfo1, conn);
                    SqlCommand cmdd2 = new SqlCommand(DInfo2, conn);
                    cmdd1.ExecuteNonQuery();
                    cmdd2.ExecuteNonQuery();
                    MessageBox.Show("换宿成功!", "提示");
                }
                else
                {
                    MessageBox.Show("对不起,您输入的学生信息未录入系统!请确认学生信息!", "提示");
                }
            }
            else
            {
                MessageBox.Show("对不起,您输入的学生信息未录入系统!请确认学生信息!", "提示");
            }
江柒七 2018-05-26
  • 打赏
  • 举报
回复
感谢 qq_37753824 您的回复,试过了您的方法,然而还是显示没有任何数据时进行无效的读取尝试
江柒七 2018-05-26
  • 打赏
  • 举报
回复
谢谢 从事CSharp程序开发 的回复,试过了您的建议,虽然不报之前的错呢,但是运行之后按了BUTTON就没反应呢,也没报错
qq_37753824 2018-05-26
  • 打赏
  • 举报
回复
while (dr.HasRows) { dr.Read(); string udINInfo = "updata INInfo2 set Sname='" + textBox2.Text + "',Grade='" + textBox3.Text + "',College='" + textBox4.Text + "',Major='" + textBox5.Text + "',Dno='" + textBox6.Text + "',INtime='" + textBox7.Text + "' where Sno='" + textBox1.Text + "'"; }
  • 打赏
  • 举报
回复
那应该是myreader也没查到值 改成

if(dr.Read())
{
            string Dno = (string)dr["Dno"];
            string sqld = "select Now from DInfo where Dno='" + textBox1.Text.Trim() + "'";
            SqlCommand cmdd = new SqlCommand(sqld, conn);
            SqlDataReader myreader = cmdd.ExecuteReader(CommandBehavior.CloseConnection);
           if(myreader.Read())
{
            int now = (int)myreader["Now"];
            if (dr.Read())
            {
                string udINInfo = "updata INInfo2 set Sname='" + textBox2.Text + "',Grade='" + textBox3.Text + "',College='" + textBox4.Text + "',Major='" + textBox5.Text + "',Dno='" + textBox6.Text + "',INtime='" + textBox7.Text + "' where Sno='" + textBox1.Text + "'";
                SqlCommand cmdud = new SqlCommand(udINInfo,conn);
                cmdud.ExecuteNonQuery();
                string insert = "insert into CHANGEInfo Sname='" + textBox2.Text + "',Grade='" + textBox3.Text + "',College='" + textBox4.Text + "',Major='" + textBox5.Text + "',Dno='" + textBox6.Text + "',INtime='" + textBox7.Text + "' where Sno='" + textBox1.Text + "'";
                SqlCommand cmdin = new SqlCommand(insert, conn);
                cmdin.ExecuteNonQuery();
                int newNow1 = now - 1;
                int newNow2 = now + 1;
                string DInfo1 = "updata DInfo set Max='6',Now='"+newNow1+"' where Dno='"+Dno+"'";
                string DInfo2 = "updata DInfo set Max='6',Now='" + newNow2 + "' where Dno='" + textBox6.Text + "'";
                SqlCommand cmdd1 = new SqlCommand(DInfo1,conn);
                SqlCommand cmdd2 = new SqlCommand(DInfo2,conn);
                cmdd1.ExecuteNonQuery();
                cmdd2.ExecuteNonQuery();
                MessageBox.Show("换宿成功!","提示");
}
else 
            {
                MessageBox.Show("对不起,您输入的学生信息未录入系统!请确认学生信息!","提示");
            }
            }
            else 
            {
                MessageBox.Show("对不起,您输入的学生信息未录入系统!请确认学生信息!","提示");
            }
}
else 
            {
                MessageBox.Show("对不起,您输入的学生信息未录入系统!请确认学生信息!","提示");
            }
江柒七 2018-05-26
  • 打赏
  • 举报
回复
感谢您的回复,但是还是不行,还是显示没有任何数据时进行无效的读取尝试
  • 打赏
  • 举报
回复
string sqlSNO = "select * from INInfo2 where Sno='" + textBox1.Text.Trim() + "'"; 这个SQL语句确定能查到数据吗? 如果不确定的话,改成

if(dr.Read())
{
            string Dno = (string)dr["Dno"];
            string sqld = "select Now from DInfo where Dno='" + textBox1.Text.Trim() + "'";
            SqlCommand cmdd = new SqlCommand(sqld, conn);
            SqlDataReader myreader = cmdd.ExecuteReader(CommandBehavior.CloseConnection);
            int now = (int)myreader["Now"];
            if (dr.Read())
            {
                string udINInfo = "updata INInfo2 set Sname='" + textBox2.Text + "',Grade='" + textBox3.Text + "',College='" + textBox4.Text + "',Major='" + textBox5.Text + "',Dno='" + textBox6.Text + "',INtime='" + textBox7.Text + "' where Sno='" + textBox1.Text + "'";
                SqlCommand cmdud = new SqlCommand(udINInfo,conn);
                cmdud.ExecuteNonQuery();
                string insert = "insert into CHANGEInfo Sname='" + textBox2.Text + "',Grade='" + textBox3.Text + "',College='" + textBox4.Text + "',Major='" + textBox5.Text + "',Dno='" + textBox6.Text + "',INtime='" + textBox7.Text + "' where Sno='" + textBox1.Text + "'";
                SqlCommand cmdin = new SqlCommand(insert, conn);
                cmdin.ExecuteNonQuery();
                int newNow1 = now - 1;
                int newNow2 = now + 1;
                string DInfo1 = "updata DInfo set Max='6',Now='"+newNow1+"' where Dno='"+Dno+"'";
                string DInfo2 = "updata DInfo set Max='6',Now='" + newNow2 + "' where Dno='" + textBox6.Text + "'";
                SqlCommand cmdd1 = new SqlCommand(DInfo1,conn);
                SqlCommand cmdd2 = new SqlCommand(DInfo2,conn);
                cmdd1.ExecuteNonQuery();
                cmdd2.ExecuteNonQuery();
                MessageBox.Show("换宿成功!","提示");
            }
            else 
            {
                MessageBox.Show("对不起,您输入的学生信息未录入系统!请确认学生信息!","提示");
            }
}
else 
            {
                MessageBox.Show("对不起,您输入的学生信息未录入系统!请确认学生信息!","提示");
            }

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧