C#.net中如何在TextBox中显示数据库查询结果?如图,点击查选,在下面的textbox显示,代码如下。。。
usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingS...
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Default3 : System.Web.UI.Page
{
private SqlConnection thisConnect;//数据库连接对象
private SqlDataAdapter thisAdapter;//对数据源的操作对象
private DataSet thisDataSet; //数据集对象
private static int Count; //当前记录指针
private SqlCommand thiscommand; //Command对象
private DataRow findRow;//行记录对象
string bhxm;//记录姓名、编号
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button3_Click(object sender, EventArgs e)
{
bhxm = TextBox7.Text.Trim();
if (RadioButton1.Checked)//按学号
{
thiscommand = new SqlCommand("select * from teacher where Tername='" + bhxm + "'", thisConnect);
}
else if (RadioButton2.Checked)//按姓名
{
thiscommand = new SqlCommand("select * from teacher where Terid='" + bhxm + "'", thisConnect);
}
//SqlDataAdapter myDataAdapter = new SqlDataAdapter();
thisAdapter = new SqlDataAdapter();
thisAdapter.SelectCommand = thiscommand;//用到查询命令
thisDataSet = new DataSet();//产生数据集
thisAdapter.Fill(thisDataSet, "thistables");
if (thisDataSet.Tables["thistables"].Rows.Count > 0) //如果有记录查到
{
TextBox1.Text = thisDataSet.Tables["thistables"].Rows[0][0].ToString();
TextBox2.Text = thisDataSet.Tables["thistables"].Rows[0][1].ToString();
TextBox3.Text = thisDataSet.Tables["thistables"].Rows[0][2].ToString();
TextBox4.Text = thisDataSet.Tables["thistables"].Rows[0][3].ToString();
TextBox5.Text = thisDataSet.Tables["thistables"].Rows[0][4].ToString();
TextBox6.Text = thisDataSet.Tables["thistables"].Rows[0][5].ToString();
}
else
{
Response.Write("<script>alert('查找不到信息,请确认输入的信息是否正确!')</script>");
}
}
} 展开
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Default3 : System.Web.UI.Page
{
private SqlConnection thisConnect;//数据库连接对象
private SqlDataAdapter thisAdapter;//对数据源的操作对象
private DataSet thisDataSet; //数据集对象
private static int Count; //当前记录指针
private SqlCommand thiscommand; //Command对象
private DataRow findRow;//行记录对象
string bhxm;//记录姓名、编号
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button3_Click(object sender, EventArgs e)
{
bhxm = TextBox7.Text.Trim();
if (RadioButton1.Checked)//按学号
{
thiscommand = new SqlCommand("select * from teacher where Tername='" + bhxm + "'", thisConnect);
}
else if (RadioButton2.Checked)//按姓名
{
thiscommand = new SqlCommand("select * from teacher where Terid='" + bhxm + "'", thisConnect);
}
//SqlDataAdapter myDataAdapter = new SqlDataAdapter();
thisAdapter = new SqlDataAdapter();
thisAdapter.SelectCommand = thiscommand;//用到查询命令
thisDataSet = new DataSet();//产生数据集
thisAdapter.Fill(thisDataSet, "thistables");
if (thisDataSet.Tables["thistables"].Rows.Count > 0) //如果有记录查到
{
TextBox1.Text = thisDataSet.Tables["thistables"].Rows[0][0].ToString();
TextBox2.Text = thisDataSet.Tables["thistables"].Rows[0][1].ToString();
TextBox3.Text = thisDataSet.Tables["thistables"].Rows[0][2].ToString();
TextBox4.Text = thisDataSet.Tables["thistables"].Rows[0][3].ToString();
TextBox5.Text = thisDataSet.Tables["thistables"].Rows[0][4].ToString();
TextBox6.Text = thisDataSet.Tables["thistables"].Rows[0][5].ToString();
}
else
{
Response.Write("<script>alert('查找不到信息,请确认输入的信息是否正确!')</script>");
}
}
} 展开
展开全部
你这个可以显示出来啊? if (thisDataSet.Tables["thistables"].Rows.Count > 0) //如果有记录查到
{
TextBox1.Text = thisDataSet.Tables["thistables"].Rows[0][0].ToString();
TextBox2.Text = thisDataSet.Tables["thistables"].Rows[0][1].ToString();
TextBox3.Text = thisDataSet.Tables["thistables"].Rows[0][2].ToString();
TextBox4.Text = thisDataSet.Tables["thistables"].Rows[0][3].ToString();
TextBox5.Text = thisDataSet.Tables["thistables"].Rows[0][4].ToString();
TextBox6.Text = thisDataSet.Tables["thistables"].Rows[0][5].ToString();
}
这几条语句是显示的啊?
为什么你这里面要这么麻烦的写法了?
thiscommand = new SqlCommand("select * from teacher where Terid='" + bhxm + "'", thisConnect);
}
//SqlDataAdapter myDataAdapter = new SqlDataAdapter();
thisAdapter = new SqlDataAdapter();
thisAdapter.SelectCommand = thiscommand;//用到查询命令
thisDataSet = new DataSet();//产生数据集
thisAdapter.Fill(thisDataSet, "thistables");
改成这样看这舒服点
SqlDataAdapter myDataAdapter = new SqlDataAdapter("select * from teacher where Terid='" + bhxm + "'", thisConnect);
DataSet thisDataSet = new DataSet();//创建数据集对象
myDataAdapter .Fill(thisDataSet );//填充数据集
if (thisDataSet.Tables[0].Rows.Count > 0) //如果有记录查到
{
TextBox1.Text = thisDataSet.Tables[0]Rows[0][0].ToString();
TextBox2.Text = thisDataSet.Tables[0]Rows[0][1].ToString();
TextBox3.Text = thisDataSet.Tables[0]Rows[0][2].ToString();
TextBox4.Text = thisDataSet.Tables[0].Rows[0][3].ToString();
TextBox5.Text = thisDataSet.Tables[0].Rows[0][4].ToString();
TextBox6.Text = thisDataSet.Tables[0].Rows[0][5].ToString();
}
else
{
Response.Write("<script>alert('查找不到信息,请确认输入的信息是否正确!')</script>");
}
{
TextBox1.Text = thisDataSet.Tables["thistables"].Rows[0][0].ToString();
TextBox2.Text = thisDataSet.Tables["thistables"].Rows[0][1].ToString();
TextBox3.Text = thisDataSet.Tables["thistables"].Rows[0][2].ToString();
TextBox4.Text = thisDataSet.Tables["thistables"].Rows[0][3].ToString();
TextBox5.Text = thisDataSet.Tables["thistables"].Rows[0][4].ToString();
TextBox6.Text = thisDataSet.Tables["thistables"].Rows[0][5].ToString();
}
这几条语句是显示的啊?
为什么你这里面要这么麻烦的写法了?
thiscommand = new SqlCommand("select * from teacher where Terid='" + bhxm + "'", thisConnect);
}
//SqlDataAdapter myDataAdapter = new SqlDataAdapter();
thisAdapter = new SqlDataAdapter();
thisAdapter.SelectCommand = thiscommand;//用到查询命令
thisDataSet = new DataSet();//产生数据集
thisAdapter.Fill(thisDataSet, "thistables");
改成这样看这舒服点
SqlDataAdapter myDataAdapter = new SqlDataAdapter("select * from teacher where Terid='" + bhxm + "'", thisConnect);
DataSet thisDataSet = new DataSet();//创建数据集对象
myDataAdapter .Fill(thisDataSet );//填充数据集
if (thisDataSet.Tables[0].Rows.Count > 0) //如果有记录查到
{
TextBox1.Text = thisDataSet.Tables[0]Rows[0][0].ToString();
TextBox2.Text = thisDataSet.Tables[0]Rows[0][1].ToString();
TextBox3.Text = thisDataSet.Tables[0]Rows[0][2].ToString();
TextBox4.Text = thisDataSet.Tables[0].Rows[0][3].ToString();
TextBox5.Text = thisDataSet.Tables[0].Rows[0][4].ToString();
TextBox6.Text = thisDataSet.Tables[0].Rows[0][5].ToString();
}
else
{
Response.Write("<script>alert('查找不到信息,请确认输入的信息是否正确!')</script>");
}
追问
它说这个错误。。。Fill: SelectCommand.Connection 属性尚未初始化。
追答
这个是你选择错了,如果你要用SqlDataAdapter 这个就不要用 SqlCommand,这个是同一级别的,
你选择SqlDataAdapter 用这个来填充数据集,就把sqlcommand 对象去掉,把连接数据库的 SqlConnection 这个放在SqlDataAdapter 这个里面,你的代码是放在sqlcommand里面,结果下面查询的时候用的是数据适配器SqlDataAdapter 这个,肯定爆错啊 ....
在加点。SqlDataAdapter 、SqlCommand 你数据库查询的时候,最好是用其中的一个。不要2个都用,SqlCommand 这个用在你查询的数据只有1个值 和1行的时候用最好,而SqlDataAdapter 这个用在你查询的数据有多行,需要把数据填充到数据集中,才用。
展开全部
首先纠正你代码书写不规范。代码贴上。自己领悟
protected void Button3_Click(object sender, EventArgs e)
{
string where = " where 1=1 ";//定义查询条件
string sql = " select * from teacher ";//查询语句
bhxm = TextBox7.Text.Trim();
if (RadioButton1.Checked)//按学号
{
where += " and Tername='" + bhxm + "' ";//组合查询条件
}
else if (RadioButton2.Checked)//按姓名
{
where += " and Terid='" + bhxm + "' ";//组合查询条件
}
thisAdapter = new SqlDataAdapter(sql,thisConnect);
thisDataSet = new DataSet();//产生数据集
thisAdapter.Fill(thisDataSet);
if (thisDataSet.Tables["thistables"].Rows.Count > 0) //如果有记录查到
{
TextBox1.Text = thisDataSet.Tables["thistables"].Rows[0][0].ToString();
TextBox2.Text = thisDataSet.Tables["thistables"].Rows[0][1].ToString();
TextBox3.Text = thisDataSet.Tables["thistables"].Rows[0][2].ToString();
TextBox4.Text = thisDataSet.Tables["thistables"].Rows[0][3].ToString();
TextBox5.Text = thisDataSet.Tables["thistables"].Rows[0][4].ToString();
TextBox6.Text = thisDataSet.Tables["thistables"].Rows[0][5].ToString();
}
else
{
Response.Write("<script>alert('查找不到信息,请确认输入的信息是否正确!')</script>");
}
}
protected void Button3_Click(object sender, EventArgs e)
{
string where = " where 1=1 ";//定义查询条件
string sql = " select * from teacher ";//查询语句
bhxm = TextBox7.Text.Trim();
if (RadioButton1.Checked)//按学号
{
where += " and Tername='" + bhxm + "' ";//组合查询条件
}
else if (RadioButton2.Checked)//按姓名
{
where += " and Terid='" + bhxm + "' ";//组合查询条件
}
thisAdapter = new SqlDataAdapter(sql,thisConnect);
thisDataSet = new DataSet();//产生数据集
thisAdapter.Fill(thisDataSet);
if (thisDataSet.Tables["thistables"].Rows.Count > 0) //如果有记录查到
{
TextBox1.Text = thisDataSet.Tables["thistables"].Rows[0][0].ToString();
TextBox2.Text = thisDataSet.Tables["thistables"].Rows[0][1].ToString();
TextBox3.Text = thisDataSet.Tables["thistables"].Rows[0][2].ToString();
TextBox4.Text = thisDataSet.Tables["thistables"].Rows[0][3].ToString();
TextBox5.Text = thisDataSet.Tables["thistables"].Rows[0][4].ToString();
TextBox6.Text = thisDataSet.Tables["thistables"].Rows[0][5].ToString();
}
else
{
Response.Write("<script>alert('查找不到信息,请确认输入的信息是否正确!')</script>");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
他这个是用的国外的一个控件,叫Devexpress,他那些文本框可以根据数据源自动生成,你如果自己要做这种效果的话就自己画上去,然后后台对他赋值就可以了.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在js脚本中实现无刷新的事件绑定
追问
不知道怎么弄。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你不是已经做了显示了吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |