C#连接数据库之SqlConnection,SqlCommand,SqldataReader
2017-12-27 22:54
441 查看
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();
}
相关文章推荐
- 菜鸟学习C#数据库实例练习使用SqlDataReader,SqlCommand SqlConnection
- 数据库操作_连接SQL Server数据库示例;连接ACCESS数据库;连接到 Oracle 数据库示例;SqlCommand 执行SQL命令示例;SqlDataReader 读取数据示例;使用DataAdapter填充数据到DataSet;使用DataTable存储数据库表;将数据库数据填充到 XML 文件;10 使用带输入参数的存储过程;11 使用带输入、输出参数的存储过程示;12 获得数据库中表的数目和名称;13 保存图片到SQL Server数据库示例;14 获得插入记录标识号;Exce
- c#学习笔记(数据库连接以及SqlDataReader、SqlCommand的使用)
- C#连接数据库sqlserver2005,执行sql,得到SqlDataReader结果的实例
- 利用SqlCommand和SqlDataReader对象操作数据库(C#)
- 利用SqlCommand和SqlDataReader对象操作数据库
- SqlConnection,SqlCommand,SqldataReader的用法总结:
- ADO.NET基础备忘1_SqlConnection SqlCommand SqlDataReader
- 利用SqlCommand和SqlDataReader对象操作数据库
- 黑马程序员_学习日记47_615数据库开发及ADO.Net(连接字符串、SqlDataReader对象、连接池)
- SqlConnection,SqlCommand,SqldataReader的用法总结:
- SqlConnection,SqlCommand,SqldataReader的用法总结:
- [转载]SqlConnection,SqlCommand,SqldataReader的用法总结:
- SqlConnection,SqlCommand,SqldataReader
- asp.net中SqlDataReader使用时关闭数据库连接的问题(转)
- SQL 数据库 C#中全局DataTable、SqlDataAdapter、SqlConnection实现实例
- C# 判断 SqlDataReader 实例是否包含查询数据库表的列
- sqlconnection,sqlcommand,sqldataadapter,sqldatareader,dataset都是做什么用的?
- C# 在一个SqlConnection中打开两个SqlDataReader
- 【数据库连接】Sqlcommand与SqlDataAdapter用法摘录(待整理)