早教吧作业答案频道 -->其他-->
举例说明ADO.NET中的几个常用的类和相应的用法。
题目详情
举例说明ADO.NET中的几个常用的类和相应的用法。
▼优质解答
答案和解析
此处假设用的是MS SQL SERVER数据库。
一.用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("tt", myReader.GetInt32(0), myReader.GetString(1));
myReader.Close();
2.使用序数索引器。
while (myReader.Read())
Console.WriteLine("tt", myReader[0].ToString(), myReader[1].ToString());
myReader.Close();
3.使用列名索引器。
while (myReader.Read())
Console.WriteLine("tt", 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); 获取指定列的字符串形式的值
一.用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("tt", myReader.GetInt32(0), myReader.GetString(1));
myReader.Close();
2.使用序数索引器。
while (myReader.Read())
Console.WriteLine("tt", myReader[0].ToString(), myReader[1].ToString());
myReader.Close();
3.使用列名索引器。
while (myReader.Read())
Console.WriteLine("tt", 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); 获取指定列的字符串形式的值
看了 举例说明ADO.NET中的几...的网友还看了以下:
晏子春秋写人往往能从三言两语之中写出人物的心理与神态请从晏子辞千金中举例说明 2020-06-12 …
大学语文记叙文《郑伯克段于鄢》一文中,举例说明细节描写在本文中对刻画人物性格的作用. 2020-06-30 …
范仲淹中举的故事谁知道?有没有范仲淹中举这回事,还是一说中举故事就是范进,没有别人发生中举的故事. 2020-07-07 …
从课文中举例说明吆喝者有着怎样的“生活的境界”,其中包含着怎样的“人生哲理”? 2020-07-14 …
有人说世说新语的语言简练含蓄又隽永传神,请从课文中举例说明这一点,急, 2020-07-25 …
萧何是个怎样的人?在"萧何月下追韩信"的故事中,举例说说,萧何诗歌怎么样的人?(越简短越好)在"萧 2020-07-28 …
英语姓名、电话的问题在英语中,举例说JameLee,JameHardyLee,就是说两个词和三个词组 2020-11-21 …
根据《范进中举》说说如果有人中了500万彩票,那人会有什么表现 2020-12-08 …
勇敢的定义只有一个,但勇敢的表现却可能多种多样.请你从生活中举例说说. 2020-12-14 …
作者说:“在文字上推敲,骨子里实在是在思想感情上‘推敲’。”理由是什么?试从自己的写作体会或者读过的 2020-12-14 …