SqlConnection,SqlCommand,SqldataReader的用法总结:
来源:互联网 发布:ie运行java未响应 编辑:程序博客网 时间:2024/05/18 03:11
SqlConnection,SqlCommand,SqldataReader的用法总结:
希望对新手有所帮助!!!
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)
{
//-------------------SqlConnection,SqlCommand,SqldataReader的用法-------------------//
//下面的4种形式等价,如果你的数据库是Windows连接方法,直接把连接字符串改成"server=.;database=northwind;integrated security=true;"
//如果你的数据库是某个实例名,直接把"server=."改成"server=你的计算机名实例名"就可以了
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=.;database=northwind;uid=sa;pwd=;"; //双引号中的最后一个分号可以去掉
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "select * from customers";
SqlDataReader sdr = cmd.ExecuteReader();
this.GridView1.DataSource = sdr;
this.GridView1.DataBind();
sdr.Close();
con.Close();
//SqlConnection con = new SqlConnection();
//con.ConnectionString = "server=.;database=northwind;uid=sa;pwd=;"; //双引号中的最后一个分号可以去掉
//con.Open();
//SqlCommand cmd = new SqlCommand("select * from customers");
//cmd.Connection = con;
//SqlDataReader sdr = cmd.ExecuteReader();
//this.GridView1.DataSource = sdr;
//this.GridView1.DataBind();
//sdr.Close();
//con.Close();
////我最经常用这一种,同时连接对象是整个程序的公共对象,所以我一般会把数据库连接封装到一个类中,这样就可以在程序的任何地方随时调用
//SqlConnection con = new SqlConnection("server=.;database=northwind;uid=sa;pwd=;"); //双引号中的最后一个分号可以去掉
//con.Open();
//SqlCommand cmd = new SqlCommand("select * from customers", con);
//SqlDataReader sdr = cmd.ExecuteReader();
//this.GridView1.DataSource = sdr;
//this.GridView1.DataBind();
//sdr.Close();
//con.Close();
//SqlConnection con = new SqlConnection();
//con.ConnectionString = "server=.;database=northwind;uid=sa;pwd=;"; //双引号中的最后一个分号可以去掉
//con.Open();
//SqlCommand cmd = new SqlCommand();
//cmd.Connection = con;
//cmd.CommandText = "select * from customers";
//cmd.CommandType = CommandType.Text; //这条语句是多余的,因为默认就是Text
//SqlDataReader sdr = cmd.ExecuteReader();
//this.GridView1.DataSource = sdr;
//this.GridView1.DataBind();
//sdr.Close();
//con.Close();
}
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)
{
//-------------------SqlConnection,SqlCommand,SqldataReader的用法-------------------//
//下面的4种形式等价,如果你的数据库是Windows连接方法,直接把连接字符串改成"server=.;database=northwind;integrated security=true;"
//如果你的数据库是某个实例名,直接把"server=."改成"server=你的计算机名实例名"就可以了
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=.;database=northwind;uid=sa;pwd=;"; //双引号中的最后一个分号可以去掉
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "select * from customers";
SqlDataReader sdr = cmd.ExecuteReader();
this.GridView1.DataSource = sdr;
this.GridView1.DataBind();
sdr.Close();
con.Close();
//SqlConnection con = new SqlConnection();
//con.ConnectionString = "server=.;database=northwind;uid=sa;pwd=;"; //双引号中的最后一个分号可以去掉
//con.Open();
//SqlCommand cmd = new SqlCommand("select * from customers");
//cmd.Connection = con;
//SqlDataReader sdr = cmd.ExecuteReader();
//this.GridView1.DataSource = sdr;
//this.GridView1.DataBind();
//sdr.Close();
//con.Close();
////我最经常用这一种,同时连接对象是整个程序的公共对象,所以我一般会把数据库连接封装到一个类中,这样就可以在程序的任何地方随时调用
//SqlConnection con = new SqlConnection("server=.;database=northwind;uid=sa;pwd=;"); //双引号中的最后一个分号可以去掉
//con.Open();
//SqlCommand cmd = new SqlCommand("select * from customers", con);
//SqlDataReader sdr = cmd.ExecuteReader();
//this.GridView1.DataSource = sdr;
//this.GridView1.DataBind();
//sdr.Close();
//con.Close();
//SqlConnection con = new SqlConnection();
//con.ConnectionString = "server=.;database=northwind;uid=sa;pwd=;"; //双引号中的最后一个分号可以去掉
//con.Open();
//SqlCommand cmd = new SqlCommand();
//cmd.Connection = con;
//cmd.CommandText = "select * from customers";
//cmd.CommandType = CommandType.Text; //这条语句是多余的,因为默认就是Text
//SqlDataReader sdr = cmd.ExecuteReader();
//this.GridView1.DataSource = sdr;
//this.GridView1.DataBind();
//sdr.Close();
//con.Close();
}
以上四种方法都可以把数据库中的数据读出来,绑定到GridView控件!!!
- SqlConnection,SqlCommand,SqldataReader的用法总结
- SqlConnection,SqlCommand,SqldataReader的用法总结:
- SqlConnection,SqlCommand,SqldataReader的用法总结:
- [转载]SqlConnection,SqlCommand,SqldataReader的用法总结:
- SqlConnection,SqlCommand,SqldataReader的用法总结:
- (转)SqlConnection,SqlCommand,SqldataReader的用法总结:
- SqlConnection,SqlCommand,SqlDataAdaper的用法总结
- SqlConnection,SqlCommand,SqlReader,SqlDataAdaper的用法总结!!
- SqlConnection,SqlCommand,SqlReader,SqlDataAdaper的用法总结!!
- SqlConnection,SqlCommand,SqlDataAdaper的用法总结
- 理解SqlConnection,SqlCommand,SqldataReader
- SqlConnection,SqlCommand,SqldataReader
- sqlconnection,sqlcommand,sqldataadapter,sqldatareader,dataset
- sqlconnection,sqlcommand,sqldataadapter,sqldatareader,dataset的关系详解
- ADO.NET 主要的三个对象(SqlConnection/SqlCommand/SqlDataReader)
- 白话SqlConnection,SqlCommand,SqlDataAdapter,SqlDataReader,DataSet
- SqlDataReader,SqlDataAdapter与SqlCommand的一点总结.
- sqlconnection,sqldataadapter,sqlcommand的区别
- vc网络编程入门
- Oracle添加修改删除表字段
- maven_部署工程到私服
- 使用Beautify.js来美化你的jQuery代码
- 破解第一课-TrackMe
- SqlConnection,SqlCommand,SqldataReader的用法总结:
- JS常见的表单输入限制方法的代码
- Flex4之弹出对话框
- DIV CSS设计时IE6/IE7/FF 兼容性问题解决篇
- 23种设计模式的形象比喻
- 如何正确地选择并加入编程领域(仅限windows系统编程)?
- C# 动态添加控件的方法
- unix环境高级编程 学习笔记5 标准IO库
- Silverlight开发历程—(利用画刷制作星光特效)