怎样判断textbox输入的数据在数据库中已存在? textbox1中输入的是id值,id是数据库中的列名,是主键,我想实现的是其余的textbox自动显示该id所在行的其余的数据, 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你在数据库中查询出textbox1中id所在数据行然后逐个将数据绑定到其它textbox不就行了 那你就在输入ID的后根据文本框的ID 把这条数据查出来不就行了 string str = "select chezhuxingming,chexing,fadongjihao,vinhao,chejiahao,goucheriqi,xinchegoujia from baoxianxinxi where chepaihao ='" + textBox1.Text + "'"; SqlConnection sqlcon = new SqlConnection("server=.;uid=sa;pwd=sa;DataBase=me"); sqlcon.Open(); SqlDataAdapter myda = new SqlDataAdapter(str, sqlcon); DataSet myds = new DataSet(); myda.Fill(myds, "baoxianguanli"); textBox2.Text = myds.Tables[0].Rows[0]["chezhuxingming"].ToString(); textBox3.Text = myds.Tables[0].Rows[0]["chexing"].ToString(); textBox4.Text = myds.Tables[0].Rows[0]["fadongjihao"].ToString(); textBox5.Text = myds.Tables[0].Rows[0]["vinhao"].ToString(); textBox6.Text = myds.Tables[0].Rows[0]["chejiahao"].ToString(); textBox8.Text = myds.Tables[0].Rows[0]["goucheriqi"].ToString(); textBox9.Text = myds.Tables[0].Rows[0]["xinchegoujia"].ToString(); sqlcon.Close();上面是我写的代码,不知道查询语句对不对,提示的错误是: textBox2.Text = myds.Tables[0].Rows[0]["chezhuxingming"].ToString()在位置0处没有任何行 高手帮看看,怎样修改?chepaihao就是我所说的id,谢谢! textBox2.Text = myds.Tables[0].Rows[0]["chezhuxingming"].ToString(); Rows[0]["chezhuxingming"] []这里放的应该是列名或是列索引值 调试,看有没有查到数据,看SQL 是否正确 这个语句没有错误,不过缺少一个判断,当表中没有数据时就会这样提示在位置0处没有任何行 。可以改过: if (myds != null && myds.Tables[0].Rows.Count > 0) { textBox2.Text = myds.Tables[0].Rows[0]["chezhuxingming"].ToString(); textBox3.Text = myds.Tables[0].Rows[0]["chexing"].ToString(); textBox4.Text = myds.Tables[0].Rows[0]["fadongjihao"].ToString(); textBox5.Text = myds.Tables[0].Rows[0]["vinhao"].ToString(); textBox6.Text = myds.Tables[0].Rows[0]["chejiahao"].ToString(); textBox8.Text = myds.Tables[0].Rows[0]["goucheriqi"].ToString(); textBox9.Text = myds.Tables[0].Rows[0]["xinchegoujia"].ToString(); } 加个判断 string str = "select chezhuxingming,chexing,fadongjihao,vinhao,chejiahao,goucheriqi,xinchegoujia from baoxianxinxi where chepaihao ='" + textBox1.Text + "'"; SqlConnection sqlcon = new SqlConnection("server=.;uid=sa;pwd=sa;DataBase=me"); sqlcon.Open(); SqlDataAdapter myda = new SqlDataAdapter(str, sqlcon); DataSet myds = new DataSet(); DataTable dt= myda.Fill(myds, "baoxianguanli"); if(dt!=null) { textBox2.Text = dt.Rows[0]["chezhuxingming"].ToString(); textBox3.Text = dt.Rows[0]["chexing"].ToString(); textBox4.Text = dt.Rows[0]["fadongjihao"].ToString(); textBox5.Text = dt.Rows[0]["vinhao"].ToString(); textBox6.Text = dt.Rows[0]["chejiahao"].ToString(); textBox8.Text = dt.Rows[0]["goucheriqi"].ToString(); textBox9.Text = myds.Tables[0].Rows[0]["xinchegoujia"].ToString(); } 加个判断 string str = "select chezhuxingming,chexing,fadongjihao,vinhao,chejiahao,goucheriqi,xinchegoujia from baoxianxinxi where chepaihao ='" + textBox1.Text + "'"; SqlConnection sqlcon = new SqlConnection("server=.;uid=sa;pwd=sa;DataBase=me"); sqlcon.Open(); SqlDataAdapter myda = new SqlDataAdapter(str, sqlcon); DataSet myds = new DataSet(); DataTable dt= myda.Fill(myds, "baoxianguanli"); if(dt!=null && dt.Rows.Count>0) { textBox2.Text = dt.Rows[0]["chezhuxingming"].ToString(); textBox3.Text = dt.Rows[0]["chexing"].ToString(); textBox4.Text = dt.Rows[0]["fadongjihao"].ToString(); textBox5.Text = dt.Rows[0]["vinhao"].ToString(); textBox6.Text = dt.Rows[0]["chejiahao"].ToString(); textBox8.Text = dt.Rows[0]["goucheriqi"].ToString(); textBox9.Text = myds.Tables[0].Rows[0]["xinchegoujia"].ToString(); } 你的数据库中没有这条记录,你可以先判断一下if(myds.Tables[0].Rows.Count>0){//写你原来的代码} 给别人做网站用 DAAB 操作数据库可以吗? 救命啊!关于sql2005 如何在父页面中创建一个子页面,并在子页面中提交数据刷新父页面? 数据库写入问题 DataSet读XML后产生的一个问题 本地的html文件是否可以调用远程服务器上的web service服务??? 给100分!!! 关于跳转页面与弹出对话框的小问题,求各位高手指点..... 参数传递问题 一个有关DataGrid的问题???????????? asp.net怎么在TextBox里面自动生成流水号?急求 .net如何用二进制形式保存文件数据到数据库中? Visible属性的问题,在线等
然后逐个将数据绑定到其它textbox不就行了
SqlConnection sqlcon = new SqlConnection("server=.;uid=sa;pwd=sa;DataBase=me");
sqlcon.Open();
SqlDataAdapter myda = new SqlDataAdapter(str, sqlcon);
DataSet myds = new DataSet();
myda.Fill(myds, "baoxianguanli");
textBox2.Text = myds.Tables[0].Rows[0]["chezhuxingming"].ToString();
textBox3.Text = myds.Tables[0].Rows[0]["chexing"].ToString();
textBox4.Text = myds.Tables[0].Rows[0]["fadongjihao"].ToString();
textBox5.Text = myds.Tables[0].Rows[0]["vinhao"].ToString();
textBox6.Text = myds.Tables[0].Rows[0]["chejiahao"].ToString();
textBox8.Text = myds.Tables[0].Rows[0]["goucheriqi"].ToString();
textBox9.Text = myds.Tables[0].Rows[0]["xinchegoujia"].ToString();
sqlcon.Close();上面是我写的代码,不知道查询语句对不对,提示的错误是: textBox2.Text = myds.Tables[0].Rows[0]["chezhuxingming"].ToString()在位置0处没有任何行
Rows[0]["chezhuxingming"] []这里放的应该是列名或是列索引值
。可以改过:
if (myds != null && myds.Tables[0].Rows.Count > 0)
{
textBox2.Text = myds.Tables[0].Rows[0]["chezhuxingming"].ToString();
textBox3.Text = myds.Tables[0].Rows[0]["chexing"].ToString();
textBox4.Text = myds.Tables[0].Rows[0]["fadongjihao"].ToString();
textBox5.Text = myds.Tables[0].Rows[0]["vinhao"].ToString();
textBox6.Text = myds.Tables[0].Rows[0]["chejiahao"].ToString();
textBox8.Text = myds.Tables[0].Rows[0]["goucheriqi"].ToString();
textBox9.Text = myds.Tables[0].Rows[0]["xinchegoujia"].ToString(); }
string str = "select chezhuxingming,chexing,fadongjihao,vinhao,chejiahao,goucheriqi,xinchegoujia from baoxianxinxi where chepaihao ='" + textBox1.Text + "'";
SqlConnection sqlcon = new SqlConnection("server=.;uid=sa;pwd=sa;DataBase=me");
sqlcon.Open();
SqlDataAdapter myda = new SqlDataAdapter(str, sqlcon);
DataSet myds = new DataSet();
DataTable dt= myda.Fill(myds, "baoxianguanli");
if(dt!=null)
{
textBox2.Text = dt.Rows[0]["chezhuxingming"].ToString();
textBox3.Text = dt.Rows[0]["chexing"].ToString();
textBox4.Text = dt.Rows[0]["fadongjihao"].ToString();
textBox5.Text = dt.Rows[0]["vinhao"].ToString();
textBox6.Text = dt.Rows[0]["chejiahao"].ToString();
textBox8.Text = dt.Rows[0]["goucheriqi"].ToString();
textBox9.Text = myds.Tables[0].Rows[0]["xinchegoujia"].ToString();
}
string str = "select chezhuxingming,chexing,fadongjihao,vinhao,chejiahao,goucheriqi,xinchegoujia from baoxianxinxi where chepaihao ='" + textBox1.Text + "'";
SqlConnection sqlcon = new SqlConnection("server=.;uid=sa;pwd=sa;DataBase=me");
sqlcon.Open();
SqlDataAdapter myda = new SqlDataAdapter(str, sqlcon);
DataSet myds = new DataSet();
DataTable dt= myda.Fill(myds, "baoxianguanli");
if(dt!=null && dt.Rows.Count>0)
{
textBox2.Text = dt.Rows[0]["chezhuxingming"].ToString();
textBox3.Text = dt.Rows[0]["chexing"].ToString();
textBox4.Text = dt.Rows[0]["fadongjihao"].ToString();
textBox5.Text = dt.Rows[0]["vinhao"].ToString();
textBox6.Text = dt.Rows[0]["chejiahao"].ToString();
textBox8.Text = dt.Rows[0]["goucheriqi"].ToString();
textBox9.Text = myds.Tables[0].Rows[0]["xinchegoujia"].ToString();
}
if(myds.Tables[0].Rows.Count>0)
{
//写你原来的代码
}