ADO.NET中的sql连接
2009-12-16 15:13
309 查看
using System.Data; // Use ADO.NET namespace
using System.Data.SqlClient;
SqlConnection thisConnection = new SqlConnection(
@"Data Source=GY; Initial Catalog=northwind;uid=sa;password=datadog"); //先建立连接
thisConnection.Open();//打开连接
SqlCommand thisCommand = thisConnection.CreateCommand();//直接指定conn的command
也可以这样 // SqlCommand cmd = new SqlCommand();
// cmd.Connection = thisConnection;
cmmand的sql命令
thisCommand.CommandText = "SELECT CustomerID, CompanyName from Customer";
SqlDataReader thisReader = thisCommand.ExecuteReader();//sqldatareader不可以用new 必须直接与command关联
//用reader读出表
while (thisReader.Read())
{
// Output ID and name columns
Console.WriteLine("/t{0}/t{1}",
thisReader["CustomerID"], thisReader["CompanyName"]);
// Close reader 用完要关闭
thisReader.Close();
// Close connection
thisConnection.Close ()
using System.Data.SqlClient;
SqlConnection thisConnection = new SqlConnection(
@"Data Source=GY; Initial Catalog=northwind;uid=sa;password=datadog"); //先建立连接
thisConnection.Open();//打开连接
SqlCommand thisCommand = thisConnection.CreateCommand();//直接指定conn的command
也可以这样 // SqlCommand cmd = new SqlCommand();
// cmd.Connection = thisConnection;
cmmand的sql命令
thisCommand.CommandText = "SELECT CustomerID, CompanyName from Customer";
SqlDataReader thisReader = thisCommand.ExecuteReader();//sqldatareader不可以用new 必须直接与command关联
//用reader读出表
while (thisReader.Read())
{
// Output ID and name columns
Console.WriteLine("/t{0}/t{1}",
thisReader["CustomerID"], thisReader["CompanyName"]);
// Close reader 用完要关闭
thisReader.Close();
// Close connection
thisConnection.Close ()
相关文章推荐
- ADO.Net基础/连接SQLServer/ExecuteNoQuery()/ExecuteScalar
- ADO.NET数据库连接、操作SQL举例
- ASP.NET连接SQL、Access、Excel数据库(一)——什么是ADO.NET
- VS2010/VS2013项目创建 ADO.NET连接mysql/sql server详细步骤
- SQL连接字符串(ADO.NET)
- ADO.NET的一个帮助类库,只须一个连接,一条SQL语句,轻松实现数据库的查旬,再加一个object 轻松实现数据库的增删改
- ASP.NET连接SQL、Access、Excel数据库(一)——什么是ADO.NET
- SqlDataAdapter适配器的用法(ADO.NET无连接模式访问数据库)
- VS2010/VS2013项目创建 ADO.NET连接mysql/sql server详细步骤
- 关于ADO.NET连接SQL一些方法
- sql 数据库连接 暨 ado.net 小例
- 黑马程序员_学习日记47_615数据库开发及ADO.Net(连接字符串、SqlDataReader对象、连接池)
- ADO.net 中数据库连接方式(sql,access,ora,db2)
- 关于ADO.NET,只须一个连接,一条SQL语句,轻松实现增删改查
- ADO.net 1 [connection 对象/连接SQL]
- ADO.NET实体框架连接串引发的异常:Unable to load the specified metadata resource
- 使用ADO.net连接msql数据库
- ADO.net,Linq to SQL和Entity Framework性能实测分析 — Windows Live
- [VB.NET]局域网内连接sql服务器(msde2000)连接不上的问题,请教!
- ADO.net,Linq to SQL和Entity Framework性能实测分析