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>");
}
}
} 展开
展开全部
第一 同学 你数据库连接字符串没有
定义了thisConnect,没有赋值啊,连接字符串没连接,后面的数据处理是白搭啊 --这个是必须的
第二 变量名称尽量有意义,这样的代码,看着很麻烦 --建议
定义了thisConnect,没有赋值啊,连接字符串没连接,后面的数据处理是白搭啊 --这个是必须的
第二 变量名称尽量有意义,这样的代码,看着很麻烦 --建议
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
thisAdapter = new SqlDataAdapter();在这之前要:
thisConnect = new sqlconnection(连接字符串);
然后调用thiscommand 的查询方法
thisConnect = new sqlconnection(连接字符串);
然后调用thiscommand 的查询方法
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在所有的操作之前需要连接数据库:
thisConnect = new sqlconnection(连接字符串);
thisConnect .Open();
thisConnect = new sqlconnection(连接字符串);
thisConnect .Open();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这样显示不了吗?你这不是都读出来并赋值给TextBox了吗
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询