ADO.NET三個核心物件的簡要說明
SqlConnection //連線字串
SqlCommand //執行sql命令的物件
SqlDataReader //讀取資料庫中的記錄
注意除了SqlConnection要記得關閉外 DataReader也要即使關閉,否則在下次使用未關閉的DataReader時候會出錯。
示例程式碼如下:
demo
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->class Program
{
///
/// 應用程式的入口
///
///
///
static void Main(string[] args)
{
string connectionString = GetConnectString();
string queryString = "select * from person where id = 2";
//string queryString = "delete from person where id = 2;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = connection.CreateCommand();
command.CommandText = queryString;
command.CommandType = CommandType.Text;
try
{
connection.Open();
//command.ExecuteNonQuery();//執行非查詢命令
SqlDataReader reader = command.ExecuteReader()//讀取資料命令;
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
Console.Write("{0}\t", reader[i]);
}
Console.WriteLine();
}
reader.Close();
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Console.Read();
}
///
/// 連線字串
///
///
public static string GetConnectString()
{
return "Data Source=(local);Init ial Catalog=mytestdb;"
+ "Integrated Security=SSPI";
}
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->class Program
{
///
/// 應用程式的入口
///
///
///
static void Main(string[] args)
{
string connectionString = GetConnectString();
string queryString = "select * from person where id = 2";
//string queryString = "delete from person where id = 2;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = connection.CreateCommand();
command.CommandText = queryString;
command.CommandType = CommandType.Text;
try
{
connection.Open();
//command.ExecuteNonQuery();//執行非查詢命令
SqlDataReader reader = command.ExecuteReader()//讀取資料命令;
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
Console.Write("{0}\t", reader[i]);
}
Console.WriteLine();
}
reader.Close();
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Console.Read();
}
///
/// 連線字串
///
///
public static string GetConnectString()
{
return "Data Source=(local);Init ial Catalog=mytestdb;"
+ "Integrated Security=SSPI";
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-526496/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SQL%ROWCOUNT的簡要說明SQL
- 簡要的CKeditor使用說明
- WordPress安裝簡要說明
- Object TreeView簡要說明 (轉)ObjectView
- CentOS 7升級核心簡明說明CentOS
- Goldengate引數簡要說明Go
- 推送mobi至Kindle流程說明(簡要操作)
- objc物件說明OBJ物件
- 蜻蜓安全工作臺程式編排簡要說明
- nagios 監控windows環境簡要說明iOSWindows
- Windows8中108個執行命令及簡要說明Windows
- js arguments物件說明JS物件
- 奈雪的茶IPO招股說明書及簡要資料
- ADO.NET中的五個主要物件物件
- 雲端計算和虛擬化的三個小區別簡單說明
- 簡要說明jquery+jcrop實現的圖片裁剪儲存jQuery
- 將本地專案上傳至GitHub上的簡要操作說明Github
- RHEL 7特性說明(四):核心
- 核心FAQ 舉例說明 (轉)
- 2020年蘋果開發者證書配置簡要說明蘋果
- Vuejs基本知識(三)【語法簡寫說明】VueJS
- 對Mysql中的read_only 只讀屬性做簡要說明MySql
- linux常用核心引數說明Linux
- pureftpd安裝配置簡明說明 (轉)FTP
- sqlmap簡單中文說明SQL
- Android draw、onDraw、dispatchDraw、invalidate、computeScroll 一些簡要說明Android
- ADO.NET物件模型物件模型
- php.ini 核心配置選項說明PHP
- linux核心裁剪選項說明Linux
- jarsigner 簡單使用說明JAR
- JavaScript字串api簡單說明JavaScript字串API
- Emacs簡易操作說明(轉)Mac
- VI 命令簡易使用說明
- suid,sgid,sticky的三個許可權的詳細說明UI
- ADO.NET的Connection物件的使用物件
- Linux核心調優部分引數說明Linux
- xming工具的簡單實用說明
- 簡要說明,實時資料分析將面臨哪些問題和挑戰?