> ** 一.用 SqlConnection连接SQL Server **
>
> ** **
>
> 1.加入命名空间
>
> using System.Data.SqlClient;
>
> 2.连接数据库
>
> SqlConnection myConnection = new SqlConnection();
> myConnection.ConnectionString = "user id=sa;password=sinofindb;initial catalog=test;data source=127.0.0.1;Connect Timeout=30";
> myConnection.Open();
>
> ** 改进(更通用)的方法: **
>
> string MySqlConnection="user id=sa;password=sinofindb;Database =test;data source=127.0.0.1;Connect Timeout=30";
> SqlConnection myConnection = new SqlConnection(MySqlConnection);
> myConnection.Open();
>
> ** 二。用 OleDbConnection连接 **
>
> ** 1.加入命名空间 **
>
> using System.Data.OleDb;
>
> ** 2.连接sql server **
>
> string MySqlConnection="Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=test;Integrated Security=SSPI;";
>
> SqlConnection myConnection = new SqlConnection(MySqlConnection);
> myConnection.Open();
>
> ** 3.连接Access(可通过建立.udl文件获得字符串) **
>
> string MySqlConnection="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\db2000.mdb;
>
> Persist Security Info=False;
>
> ** 4.连接Oracle(也可通过OracleConnection连接) **
>
> string MySqlConnection="Provider=MSDAORA;Data Source=db; user id=sa;password=sinofindb";
>
> ** 三 .创建Command对象 **
>
> ** **
>
> ** 1. ** ** SqlCommand 构造函数 ** ** **
>
> ①初始化 SqlCommand 类的新实例。 public SqlCommand();
>
> SqlCommand myCommand = new SqlCommand();
>
> ②初始化具有查询文本的 SqlCommand 类的新实例。 public SqlCommand(string);
>
>
> String mySelectQuery = "SELECT * FROM mindata";
>
>
> SqlCommand myCommand = new SqlCommand(mySelectQuery);
>
> ③初始化具有查询文本和 SqlConnection 的SqlCommand类实例。
>
> Public SqlCommand(string, SqlConnection);
>
>>
>> String mySelectQuery = "SELECT * FROM mindata";
>>
>>
>> string myConnectString = "user id=sa;password=;database=test;server=mySQLServer";
>>
>>
>> SqlConnection myConnection = new SqlConnection(myConnectString);
>>
>>
>> SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
>
> ④初始化具有查询文本、SqlConnection 和 Transaction 的 SqlCommand 类实例。
>
> public SqlCommand(string, SqlConnection, SqlTransaction);
>
>>
>> SqlTransaction myTrans = myConnection.BeginTransaction();
>>
>>
>> String mySelectQuery = "SELECT * FROM mindata";
>>
>>
>> string myConnectString = "user id=sa;password=;database=test;server=mySQLServer";
>>
>>
>> SqlConnection myConnection = new SqlConnection(myConnectString);
>>
>>
>> SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection, myTrans);
>
> ** 2.建立 ** ** SqlCommand ** ** 与 ** ** SqlConnection的关联。 **
>
> myCommand.Connection = myConnection;
>
> 或者: SqlCommand myCommand = myConnection.CreateCommand;
>
> ** 3.设置SqlCommand的查询文本。 **
>
> myCommand.CommandText = "SELECT * FROM mindata";
>
> 或者第 2种构造:SqlCommand myCommand = new SqlCommand(mySelectQuery);
>
> 给 SqlCommand对象提供两个查询字符串,每个查询字符串访问不同的表,返回不同的结果集。
>
> 两个查询语句用分号分隔。
>
> ** 4. 执行命令。 **
>
> ExecuteReader
>
> |
>
> 返回一行或多行
>
> ---|---
>
> ExecuteNonQuery
>
> |
>
> 对 Connection 执行 Transact-SQL 语句并返回受影响的行数(int)
>
> ExecuteScalar
>
> |
>
> 返回单个值 ( 如一个聚合值 ). 返回结果集中第一行的第一列。忽略额外的列或行
>
> ExecuteXmlReader
>
> |
>
> 将 CommandText 发送到 Connection 并生成一个 XmlReader 对象。
>
> SqlDataReader myReader = myCommand.ExecuteReader();
>
> 或 SqlDataReader myReader = myCommand.ExecuteReader( CommandBehavior.CloseConnection );
>
>
> while(myReader.Read()) //循环读取数据
>
>
> {
>
>
> Console.WriteLine(myReader.GetString(0));// 获取指定列的字符串形式的值
>
>
> Console.WriteLine(myReader. GetValue(1));// 获取以本机格式表示的指定列的值
>
>
> }
>
>
> CommandText = "select count(*) as NumberOfRegions from region";
>
> Int count = (int) myCommand .ExecuteScalar();
>
> 关于 OleDbCommand对象的使用。
>
> ** 四. DataReader的使用 **
>
> ** **
>
> ** 1.遍历结果集 **
>
> while (myReader.Read())
>
> Console.WriteLine("\t{0}\t{1}", myReader.GetInt32(0), myReader.GetString(1));
>
> myReader.Close();
>
> ** 2.使用序数索引器。 **
>
> while (myReader.Read())
>
> Console.WriteLine("\t{0}\t{1}", myReader[0].ToString(), myReader[1].ToString());
>
> myReader.Close();
>
> ** 3.使用列名索引器。 **
>
> while (myReader.Read())
>
> Console.WriteLine("\t{0}\t{1}", myReader["code].ToString(), myReader["name"].ToString());
>
> myReader.Close();
>
> ** 4.使用类型访问器 ** 。
>
> public char GetChar( _ int _ __ i _ _ ); 获取指定列的单个字符串形式的值
>
> public DateTime GetDateTime( _ int _ _ _ i _ _ ); 获取指定列的 DateTime 对象形式的值
>
> public short GetInt16( _ int _ __ i _ _ ); 获取指定列的 16 位有符号整数形式的 [C#]
>
> public string GetString( _ int _ __ i _ _ ); 获取指定列的字符串形式的值
>
> ** 5 ** ** .得到列信息 ** 。
>
> myReader .FieldCount 获取当前行中的列数
>
> myReader .GetFieldType( 序号 ) 获取是对象的数据类型的 _ Type _
>
> myReader .GetDataTypeName( 序号 ) 获取源数据类型的名称
>
> myReader .GetName( 序号 ) 获取指定列的名称
>
> myReader .GetOrdinal( 序号 ) 在给定列名称的情况下获取列序号
>
> ** 6. ** ** 得到数据表的信息。 **
>
> myReader . GetSchemaTable() 返回一个 DataTable
>
> ** 7 ** ** .操作多个结果集 ** 。
>
> myReader . NextResult() 使数据读取器前进到下一个结果集
>
> do
>
> {
>
> while (myReader.Read())
>
> Console.WriteLine("\t{0}\t{1}", myReader.GetInt32(0), myReader.GetString(1));
>
> }
>
> while( myReader . NextResult() );
>
> ** 五. ** ** DataAdapter **
>
> ** **
>
> ** 1. ** ** 创建 SqlDataAdapter **
>
> 初始化 SqlDataAdapter 类的新实例。
>
> public SqlDataAdapter();
>
> 将指定的 SqlCommand 作为 SelectCommand 属性,初始化 SqlDataAdapter 类的新实例。
>
> public SqlDataAdapter(SqlCommand);
>
> 用 selectcommand 字符串 和 SqlConnection 对象初始化 SqlDataAdapter 类的新实例。
>
> public SqlDataAdapter(string, SqlConnection) ;
>
> 用 selectcommand 字符串 和 一个连接字符串 初始化 SqlDataAdapter 类的新实例。
>
> public SqlDataAdapter(string, string);
>
> ** 2. DataAdapter ** ** 和 ** ** SqlConnection ** ** , SqlCommand ** ** 建立关联。 **
>
> 1. DataAdapter 在构造参数时建立
>
> 2 . SqlDataAdapter adapter = new SqlDataAdapter();
>
> adapter.SelectCommand = new SqlCommand(query, conn);
>
> ** 3. DataAdapter.Fill ** ** ()方法。 **
>
> 在 DataSet 中添加或刷新
数据库开发个人总结(ADO.NET)
comments powered by Disqus