以前写机房收费系统的时候数据库连接这一块就没怎么弄明白,这次有机会了得把这部分内容好好看看···
对于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# Database 类。包括 SqlConnection,SqlCommand,SqlDataReader 的使用
SqlConnection类用于建立与数据库的连接,SqlCommand类用于创建SQL命令,SqlDataReader对象是执行SqlCommand对象返回结果集的查询语句后的结果。 下面是部分代码: /*其中server表示服务器,“.”表示当地服务器,...
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); while(sqlDataReader.Read()) { //Get KeywordID and KeywordName , You can do anything you like. Here I just output them. ...
首先创建SqlConnection类和SqlCommand类实例,分别用于连接到SQL Server数据库和执行SQL语句命令,然后打开数据连接并使用SqlCommand的相应方法执行SQL语句;ExecuteNonQuery()方法执行SQL语句并且不返回数据;...
刚接触C#的新手,首先面对VS与SQL数据库连接问题。...代码有面向对象思想,调用类去获得connection连接,并且给出详细的SqlConnection、SqlCommand、SqlDataReader的关系和意义。对于新手理解数据库连接十分有用。
如果不使用数据访问层,那么你的代码里会出现很多SqlConnection、SqlCommand、SqlDataReader、Open、 Close……这些类和方法,而且代码量很大,让你不胜其烦,而且代码写起来,其实都是体力活,没有技术含量。
SqlConnection con=new SqlConnection(DataAccess.ConnectionString); SqlCommand cmd=new SqlCommand(sql,con); SqlDataReader dr=null; try { con.Open(); dr=cmd.ExecuteReader...
SqlCommand cmd = new SqlCommand(safeSql, Connection); int result = cmd.ExecuteNonQuery(); return result; } public static int ExecuteCommand(string sql, params SqlParameter[] values) { ...
using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand(SQLString, connection)) { try { connection.Open(); cmd.CommandTimeout = Times; ...
SqlConnection con = new SqlConnection(StrConnect); SqlCommand cmd = new SqlCommand(); try { PrepareCommand(cmd, con, cmdType, cmdText, paras); SqlDataReader reader = cmd.ExecuteReader...
/// 参数数组</param> /// <returns>SqlDataReader读取器</returns> public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] parameters) { SqlConnection conn = new SqlConnection(connStr...
SqlHelper用于简化你重复的去写那些数据库连接(SqlConnection),SqlCommand,SqlDataReader等等。SqlHelper 封装过后通常是只需要给方法传入一些参数如数据库连接字符串,SQL参数等,就可以访问数据库了,很方便。
为了简化重复的去写那些数据库连接(SqlConnection),SqlCommand,SqlDataReader等等,在参照了SqlHelper类的基础上重新设计了一个通用.net平台数据操作组件,该组件对ADO.Net进行封装后,通常是只需要给方法传入一些...
SqlHelper用于简化你重复的去写那些数据库连接(SqlConnection),SqlCommand,SqlDataReader等等。SqlHelper 封装过后通常是只需要给方法传入一些参数如数据库连接字符串,SQL参数等,就可以访问数据库了,很方便。
SqlHelper用于简化你重复的去写那些数据库连接(SqlConnection),SqlCommand,SqlDataReader等等。SqlHelper 封装过后通常是只需要给方法传入一些参数如数据库连接字符串,SQL参数等,就可以访问数据库了,很方便。
2、连接字符串的写法 3、SqlConnection对象 4、SqlCommand对象 5、SqlDataReader对象 6、DataSet对象 7、释放资源 1、概述 ado.net提供了丰富的数据库操作,这些操作可以分为三个步骤: 第一,使用SqlConnection...
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); ...
using (SqlConnection connection = new SqlConnection(@"server=.;database=gr;user id=sa;pwd=")) { //SqlDataAdapter sda = new SqlDataAdapter("select * from customers", connection); //DataSet ds = new ...
SqlCommand cmd = new SqlCommand(); SqlConnection conn = new SqlConnection(connstr); // 我们在这里用 try/catch 是因为如果这个方法抛出异常,我们目的是关闭数据库连接,再抛出异常, // 因为这时不会有...