`
dengyll
  • 浏览: 90442 次
社区版块
存档分类
最新评论

理解SqlConnection,SqlCommand,SqldataReader

阅读更多

    以前写机房收费系统的时候数据库连接这一块就没怎么弄明白,这次有机会了得把这部分内容好好看看···

       对于SqlConnection,SqlCommand,SqldataReader的使用和他们之间的关系不是很清楚下面对SqlConnection,SqlCommand,SqldataReader的几种配合使用的方式进行了总结:


       第一种:
        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# SqlConnection

    自己常用的C# Database 类。包括 SqlConnection,SqlCommand,SqlDataReader 的使用

    C#访问SQL Server数据库的实现方法

    SqlConnection类用于建立与数据库的连接,SqlCommand类用于创建SQL命令,SqlDataReader对象是执行SqlCommand对象返回结果集的查询语句后的结果。 下面是部分代码: /*其中server表示服务器,“.”表示当地服务器,...

    c#数据库操作的3种典型用法

    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); while(sqlDataReader.Read()) { //Get KeywordID and KeywordName , You can do anything you like. Here I just output them. ...

    .NET连接MS数据库的类

    首先创建SqlConnection类和SqlCommand类实例,分别用于连接到SQL Server数据库和执行SQL语句命令,然后打开数据连接并使用SqlCommand的相应方法执行SQL语句;ExecuteNonQuery()方法执行SQL语句并且不返回数据;...

    C#的GUI数据库连接

    刚接触C#的新手,首先面对VS与SQL数据库连接问题。...代码有面向对象思想,调用类去获得connection连接,并且给出详细的SqlConnection、SqlCommand、SqlDataReader的关系和意义。对于新手理解数据库连接十分有用。

    asp.net SqlHelper数据访问层的使用

    如果不使用数据访问层,那么你的代码里会出现很多SqlConnection、SqlCommand、SqlDataReader、Open、 Close……这些类和方法,而且代码量很大,让你不胜其烦,而且代码写起来,其实都是体力活,没有技术含量。

    C#源代码 学生成绩管理系统

    SqlConnection con=new SqlConnection(DataAccess.ConnectionString); SqlCommand cmd=new SqlCommand(sql,con); SqlDataReader dr=null; try { con.Open(); dr=cmd.ExecuteReader...

    DBHelper方便连接数据库 DBHelper

    SqlCommand cmd = new SqlCommand(safeSql, Connection); int result = cmd.ExecuteNonQuery(); return result; } public static int ExecuteCommand(string sql, params SqlParameter[] values) { ...

    CMS.DBUtility.dll

    using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand(SQLString, connection)) { try { connection.Open(); cmd.CommandTimeout = Times; ...

    SQLHelper.cs

    SqlConnection con = new SqlConnection(StrConnect); SqlCommand cmd = new SqlCommand(); try { PrepareCommand(cmd, con, cmdType, cmdText, paras); SqlDataReader reader = cmd.ExecuteReader...

    菜品管理数据库.doc

    /// 参数数组</param> /// <returns>SqlDataReader读取器</returns> public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] parameters) { SqlConnection conn = new SqlConnection(connStr...

    VB.NET实现SqlHelper数据库操作组件

    SqlHelper用于简化你重复的去写那些数据库连接(SqlConnection),SqlCommand,SqlDataReader等等。SqlHelper 封装过后通常是只需要给方法传入一些参数如数据库连接字符串,SQL参数等,就可以访问数据库了,很方便。

    c# 数据库操作组件

    为了简化重复的去写那些数据库连接(SqlConnection),SqlCommand,SqlDataReader等等,在参照了SqlHelper类的基础上重新设计了一个通用.net平台数据操作组件,该组件对ADO.Net进行封装后,通常是只需要给方法传入一些...

    Sqlhelper数据库连接字符串

    SqlHelper用于简化你重复的去写那些数据库连接(SqlConnection),SqlCommand,SqlDataReader等等。SqlHelper 封装过后通常是只需要给方法传入一些参数如数据库连接字符串,SQL参数等,就可以访问数据库了,很方便。

    SQLHelper.cs非常全,还带有注释说明!

    SqlHelper用于简化你重复的去写那些数据库连接(SqlConnection),SqlCommand,SqlDataReader等等。SqlHelper 封装过后通常是只需要给方法传入一些参数如数据库连接字符串,SQL参数等,就可以访问数据库了,很方便。

    C#-数据库知识点及连接数据库方法.docx

    2、连接字符串的写法 3、SqlConnection对象 4、SqlCommand对象 5、SqlDataReader对象 6、DataSet对象 7、释放资源 1、概述 ado.net提供了丰富的数据库操作,这些操作可以分为三个步骤: 第一,使用SqlConnection...

    C# 手机销售系统

    SqlConnection sqlCon= new SqlConnection("server=.;Integrated Security=SSPI;Database=MTSSystem"); return sqlCon; } public static DataTable selectMobile() { SqlConnection con=DataBL....

    通过查询数据库里的方法名称,来调用方法

    SqlConnection conn = new SqlConnection("server=.;uid =sa;pwd=;database=;"); string str = "select * from [KeyWord] where id='1'"; conn.Open(); SqlCommand cmd = new SqlCommand(str, conn); ...

    DataGridview绑定

    using (SqlConnection connection = new SqlConnection(@"server=.;database=gr;user id=sa;pwd=")) { //SqlDataAdapter sda = new SqlDataAdapter("select * from customers", connection); //DataSet ds = new ...

    .net 数据库访问类

    SqlCommand cmd = new SqlCommand(); SqlConnection conn = new SqlConnection(connstr); // 我们在这里用 try/catch 是因为如果这个方法抛出异常,我们目的是关闭数据库连接,再抛出异常, // 因为这时不会有...

Global site tag (gtag.js) - Google Analytics