马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

C#连接SQL Server查询的数据当查询据库里没有时vs2005报错

[复制链接]
查看3411 | 回复1 | 2013-10-15 22:57:36 | 显示全部楼层 |阅读模式
  private void chaxu()
        {
            string myConStr = "Data Source=.;Initial Catalog=wo;Integrated Security=True";
            SqlConnection myCon = new SqlConnection(myConStr);
            myCon.Open();
            SqlCommand myCom = new SqlCommand();
            myCom.CommandType = CommandType.Text;
            string comStr = "select 成员编号,地址编号,姓氏,名字,角色,发送贺卡 from 家庭成员";
            comStr = comStr + " where 成员编号='" + textBox1.Text.ToString() + "'";
            myCom.CommandText = comStr;
            myCom.Connection = myCon;
            SqlDataReader myRead = myCom.ExecuteReader();
            myRead.Read();
           
                this.textBox1.Text = myRead.GetInt32(0).ToString();
                this.textBox2.Text = myRead.GetInt32(1).ToString();
                this.textBox3.Text = myRead.GetString(2);
                this.textBox4.Text = myRead.GetString(3);
                this.textBox5.Text = myRead.GetString(4);
                this.textBox6.Text = myRead.GetBoolean(5).ToString();
            
            myRead.Close();
            myCon.Close();
        }
这个是查询函数



我是学生QQ2362910894 真心求个解决方法
ibcadmin | 2013-10-16 14:08:36 | 显示全部楼层
this.textBox1.Text = myRead.GetInt32(0).ToString();
                 this.textBox2.Text = myRead.GetInt32(1).ToString();
                 this.textBox3.Text = myRead.GetString(2);
                 this.textBox4.Text = myRead.GetString(3);
                 this.textBox5.Text = myRead.GetString(4);
                 this.textBox6.Text = myRead.GetBoolean(5).ToString();

赋值的时候 这里加个判断  这样写

if(myRead.Read())
{
this.textBox1.Text = myRead.GetInt32(0).ToString();
                 this.textBox2.Text = myRead.GetInt32(1).ToString();
                 this.textBox3.Text = myRead.GetString(2);
                 this.textBox4.Text = myRead.GetString(3);
                 this.textBox5.Text = myRead.GetString(4);
                 this.textBox6.Text = myRead.GetBoolean(5).ToString();


}
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则