ADO.NET之command查询数据
来源:互联网 发布:小河淌水 知乎 编辑:程序博客网 时间:2024/04/30 07:20
执行结果:
C# ->新建空网站->添加新项WEB窗体
cs代码:
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.bind();
}
}
public SqlConnection GetConnection()
{
string Str = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection Conn = new SqlConnection(Str);
return Conn;
}
protected void bind()
{
SqlConnection myConn = GetConnection();
myConn.Open();
string sqlStr = "select * from t001";
SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
GridView1.DataSource = myDs;
GridView1.DataBind();
myDa.Dispose();
myDs.Dispose();
myConn.Close();
}
protected void btnSelect_Click(object sender, EventArgs e)
{
if (this.txtName.Text != "")
{
SqlConnection myConn = GetConnection();
myConn.Open();
string sqlStr = "select * from t001 where bukrs=@Name";
SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
myCmd.Parameters.Add("@Name", SqlDbType.VarChar, 20).Value = this.txtName.Text.Trim();
SqlDataAdapter myDa = new SqlDataAdapter(myCmd);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
if (myDs.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = myDs;
GridView1.DataBind();
}
else
{
Response.Write("<script>alert('没有相关记录')</script>");
}
myDa.Dispose();
myDs.Dispose();
myConn.Close();
}
else
this.bind();
}
}
配置文件代码:
<configuration>
<appSettings>
<add key="ConnectionString" value="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\DataBase\sap.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"/>
</appSettings>
<connectionStrings/>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<authentication mode="Windows"/>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>
</configuration>
- ADO.NET之command查询数据
- ADO.NET之command添加数据
- ADO.NET之command更新数据
- ADO.NET之command更新数据(视图版)
- ADO.NET之command录入数据(视图版)
- ADO.NET之command录入数据-视图同步更新
- ADO.NET 数据连接查询
- ADO.NET查询表数据
- ADO.NET数据查询方法
- ADO.NET之3-Command对象---ShinePans
- ADO.NET之6-使用Command修改数据库中的数据---ShinePans
- ADO.NET数据访问(2) 查询数据
- ADO.NET--Command对象
- ADO.net之导入数据
- ADO.NET查询数据超时解决方法
- 【查】使用ADO .NET查询数据
- ADO.NET-查询和检索数据
- ADO.NET常用对象详解之:Command对象
- 最基本的商业模式
- java线程池
- MIT算法导论-第10讲-2-3树、红黑树
- Android Volley完全解析
- 头像变圆圈并转动代码
- ADO.NET之command查询数据
- java序列化反序列化uid
- Android创建Virtual Devices时出错
- 让nginx支持thinkphp的rewrite模式(url_model为2)
- C#全局键盘监听
- 大数据性能调优之HBase的RowKey设计
- apache kafka系列之-监控指标
- android 无线调试
- String[]转换List后增加元素