asp.net如何查询数据库信息

aicainiao_110 2009-12-23 11:33:58
(请给一段完整可行的代码)我想要用asp.net C#查询sql server 的完整程序段
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace WebApplication2
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox;
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected System.Data.SqlClient.SqlCommand sqlCommand1;
protected System.Web.UI.WebControls.DataGrid Grid1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面

SqlConnection con = new SqlConnection("server=XCH;database=aaa;uid=sa;pwd=");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select *from Table1", con);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//

//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlCommand1 = new System.Data.SqlClient.SqlCommand();
this.Grid1.SelectedIndexChanged += new System.EventHandler(this.Grid1_SelectedIndexChanged);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{

}

private void sqlConnection1_InfoMessage(object sender, System.Data.SqlClient.SqlInfoMessageEventArgs e)
{

}

private void Grid1_SelectedIndexChanged(object sender, System.EventArgs e)
{


}

}
}

...全文
489 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
silentwins 2009-12-23
  • 打赏
  • 举报
回复
       SqlConnection con = new SqlConnection("server=XCH;database=aaa;uid=sa;pwd=");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select *from Table1", con);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
this.Grid1.DataSource = ds.Tables[0];
this.Grid1.DataBind();
aicainiao_110 2009-12-23
  • 打赏
  • 举报
回复
我知道 可是我现在用帮我改改吧 谢谢
mngzilin 2009-12-23
  • 打赏
  • 举报
回复
你现在主要是sql语句不熟悉,其实上面的回复都很好。建议你去学习sql
misssdf 2009-12-23
  • 打赏
  • 举报
回复
你把有关查询的写在一个方法里面,方法的参数就用sql语句作为参数,返回类型什么的根据你的sql语句,sql语句作为字符串写在按钮的事件里,当点击按钮的时候,就调用这个方法。
aicainiao_110 2009-12-23
  • 打赏
  • 举报
回复
本人在线等回复
aicainiao_110 2009-12-23
  • 打赏
  • 举报
回复
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace WebApplication3
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid Grid1;
protected System.Web.UI.WebControls.TextBox TextBox;
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面


}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Grid1.SelectedIndexChanged += new System.EventHandler(this.Grid1_SelectedIndexChanged);
this.Button1.Click += new System.EventHandler(this.Button1_Click);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
SqlConnection con = new SqlConnection("server=XCH;database=aaa;uid=sa;pwd=");
SqlDataAdapter da = new SqlDataAdapter("select *from Table1 where id=1",con);
DataSet ds = new DataSet();
da.Fill(ds);
this.Grid1.DataSource = ds;
this.Grid1.DataBind();
}
private void Grid1_SelectedIndexChanged(object sender, System.EventArgs e)
{

}
}
}
调试成功 但不显示查询的数据 怎么 回事 跪求帮助
aicainiao_110 2009-12-23
  • 打赏
  • 举报
回复
能写个完整的么 谢谢
zhulong1111 2009-12-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 aicainiao_110 的回复:]
private void Button1_Click(object sender, System.EventArgs e)
{
SqlConnection con = new SqlConnection("server=XCH;database=aaa;uid=sa;pwd=");
SqlDataAdapter da = new SqlDataAdapter("select *from Table1", con);
DataSet ds = new DataSet();
da.Fill(ds);
this.Grid1.DataSource = ds.Tables[0];
this.Grid1.DataBind();
}[/Quote]

SqlDataAdapter da = new SqlDataAdapter("select *from Table1", con);

sql语句加个条件撒
aicainiao_110 2009-12-23
  • 打赏
  • 举报
回复
		private void Button1_Click(object sender, System.EventArgs e)
{
SqlConnection con = new SqlConnection("server=XCH;database=aaa;uid=sa;pwd=");
SqlDataAdapter da = new SqlDataAdapter("select *from Table1", con);
DataSet ds = new DataSet();
da.Fill(ds);
this.Grid1.DataSource = ds.Tables[0];
this.Grid1.DataBind();
}
这样显示全部数据 怎么显示要查询的数据啊
于吉小妖 2009-12-23
  • 打赏
  • 举报
回复
建议看看电子书吧。。除了helloword等不与数据库打交道的 这是C# 最基础 的代码
aicainiao_110 2009-12-23
  • 打赏
  • 举报
回复
你那个不行
huxy362428 2009-12-23
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ExamDAl
{
public class DBHelp
{
public static SqlConnection con;
public static SqlConnection Con
{
get
{
string str = "Server=20090613-2349;database=OnlineExamSystem;Uid=sa;pwd=362427";
if(con==null)
{
con=new SqlConnection(str);
con.Open();
}else if(con.State==ConnectionState.Closed)
{
con.Open();
}else if(con.State==ConnectionState.Broken)
{
con.Close();
con.Open();
}
return con;
}

}
public static DataTable findAll(string sql)
{
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(sql,Con);
da.Fill(dt);
return dt;
}
}
}
/// <summary>
/// 查询所有
/// </summary>
/// <returns></returns>
public static List<Question> findAllQuestions()
{
List<Question> list = new List<Question>();
string sql = "select * from Questions";
DataTable dt = DBHelp.findAll(sql);
foreach(DataRow dr in dt.Rows)
{
Question q = new Question();
q.Question1 = Convert.ToString(dr["Question"]);
q.A = Convert.ToString(dr["A"]);
q.B = Convert.ToString(dr["B"]);
q.C = Convert.ToString(dr["C"]);
q.D = Convert.ToString(dr["D"]);
q.Answer = Convert.ToString(dr["Answer"]);
list.Add(q);
}
return list;
}
只供参考
aicainiao_110 2009-12-23
  • 打赏
  • 举报
回复
并把显示的数据改变字体颜色
aicainiao_110 2009-12-23
  • 打赏
  • 举报
回复
我想把查询到的数据在DataGrid中显示出来怎么写
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace WebApplication3
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid Grid1;
protected System.Web.UI.WebControls.TextBox TextBox;
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
SqlConnection con = new SqlConnection("server=XCH;database=aaa;uid=sa;pwd=");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select *from Table1", con);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
this.Grid1.DataSource = ds.Tables[0];
this.Grid1.DataBind();

}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Grid1.SelectedIndexChanged += new System.EventHandler(this.Grid1_SelectedIndexChanged);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{

}

private void Grid1_SelectedIndexChanged(object sender, System.EventArgs e)
{

}
}
}

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧