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)
{


}

}
}

...全文
519 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)
{

}
}
}
源码链接: https://pan.quark.cn/s/53617c93469f IGES(Initial Graphics Exchange Specification)是一种开放性的标准格式,用于在不同CAD(计算机辅助设计)系统之间进行数据交换。该格式支持用户在不同软件平台之间传输3D几何模型,进而达成跨平台的设计共享与协同作业。IGES查看器是专门设计用来处理这些IGES文件的工具,而“IGES查看器”即是一个应用程序,它能够协助用户直接打开并检视IGES或igs(IGES的文件扩展名)文件。在CAD领域中,数据交换是一项核心议题,因为不同的设计软件通常采用各自独有的专有格式来储存几何数据。IGES格式的问世,有效应对了这一问题,使得数据能够在Autodesk的AutoCAD、SolidWorks、CATIA、UGS NX等多种CAD软件之间进行传输。这种格式能够兼容多种3D几何类型,涵盖线框、曲面以及实体模型,同时还包括注释、尺寸和其他非几何信息。“IGES查看器”作为一个独立的应用程序,其主要功能涵盖以下方面:1. **文件打开与浏览**:用户可借助该工具直接打开IGES或igs文件,无需借助原始的CAD软件。这对于未安装CAD软件或需要迅速预览模型的用户而言极为便利。2. **3D模型显示**:它能够展示IGES文件中的3D几何模型,包括线框、曲面和实体,用户可从多个视角检视模型,并调整旋转、平移和缩放,以获得全面的视觉感受。3. **视图操作**:提供多样化的视图控制选项,如正视图、侧视图、俯视图,以及自由视角,帮助用户更清晰地理解模型结构。4. **模型分析**:可能包含测量功能,例如长度、宽度、高度的测量,或角度、距离的计算,便于对模型...
内容概要:本文围绕一篇发表于超级顶刊TAC的长文展开复现研究,聚焦于线性二次型调节器(LQR)的直接自适应学习方法,提出了一种数据驱动的策略优化框架。该研究摆脱了传统控制方法对精确系统模型的依赖,通过引入在线数据进行策略迭代与优化,实现了在未知或部分已知动态系统下的高性能控制。文中详细阐述了算法的设计原理、收敛性分析及其实现流程,并提供了完整的Matlab代码实现,便于读者验证与进一步拓展。研究体现了现代控制理论向数据驱动、自适应方向发展的趋势,具有较强的理论深度与工程应用价值。; 适合人群:具备自动控制理论基础、优化算法背景及Matlab编程能力的研究生、博士生及从事控制领域科研工作的工程师,尤其适合致力于自适应控制、强化学习与最优控制交叉方向的研究者。; 使用场景及目标:①深入理解LQR问题的数据驱动求解范式;②掌握策略迭代、自适应动态规划在最优控制中的实现方法;③复现顶刊成果,支撑高水平论文写作与科研项目申报;④为复杂系统无模型控制提供技术储备与实践参考。; 阅读建议:建议读者结合TAC原文进行对照学习,重点关注算法推导与代码实现之间的映射关系,动手调试代码以加深对参数设置、收敛条件及数值稳定性的理解,同时可尝试将其迁移至其他控制任务中进行验证与改进。
打开链接下载源码: https://pan.quark.cn/s/a4b39357ea24 连续投影算法(Sequential Projection Algorithm, SAP)属于一种在线性降维领域的高维数据分析技术,其目的是寻找低维子空间,并且这种技术经常被应用于特征选择和数据可视化等场景。MATLAB作为一个功能强大的数值计算和科学计算平台,是执行此类算法的一个非常适合的环境。在此压缩包中,包含了基于MATLAB的SAP算法的具体实现,用户需要先将要处理的数据存入Excel文件,然后通过执行MATLAB程序来启动计算过程。SAP算法的核心概念在于采用迭代方法,每一次迭代中都会挑选一个与当前子空间正交的新向量,以此来逐步构建出一个低维的投影空间。这个迭代过程会持续进行,直到所有关键特征都被纳入考虑范围或者达到了事先设定的降维标准。SAP算法的一个显著好处在于其操作简便和运行高效,能够在尽可能保留原始信息的前提下,有效降低计算的复杂度。在MATLAB程序中运用SAP算法通常需要遵循以下几个步骤:1. **数据准备**:用户必须在Excel中正确地组织数据,确保所有数据都是数值型的,并且每个样本数据应该分布在不同的列中,每行则对应一个特征。2. **数据导入**:在MATLAB环境中,用户可以利用`readtable`或`xlsread`等函数来导入存储在Excel文件中的数据。3. **调用SAP函数**:用户需要执行专门为SAP算法编写的MATLAB函数,并将数据矩阵作为参数传递给该函数。函数内部会进行一系列迭代计算,以确定最佳的低维投影方案。4. **结果分析**:在计算完成后,SAP函数会输出一个已经降维的数据矩阵,这个矩阵可以用来进行后续的数据分析、模型...

62,264

社区成员

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

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

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

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