C#连接SQL Server查询的数据当查询据库里没有时vs2005报错
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();
}
这个是查询函数
http://user.qzone.qq.com/1371578434/infocenter#!app=4&via=QZ.HashRefresh
http://user.qzone.qq.com/1371578434/infocenter#!app=4&via=QZ.HashRefreshhttp://user.qzone.qq.com/1371578434/infocenter#!app=4&via=QZ.HashRefresh
http://user.qzone.qq.com/1371578434/infocenter#!app=4&via=QZ.HashRefresh
我是学生QQ2362910894 真心求个解决方法 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();
}
页:
[1]