国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

數據庫開發總結(ADO.NET小結)

2024-07-10 12:40:24
字體:
來源:轉載
供稿:網友
一.用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";
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 商都县| 临洮县| 丹寨县| 南皮县| 中宁县| 靖安县| 通城县| 德庆县| 玉山县| 磐石市| 连城县| 峡江县| 辽宁省| 古田县| 惠安县| 临泉县| 博湖县| 遂溪县| 太仆寺旗| 和静县| 海林市| 锦屏县| 新巴尔虎右旗| 错那县| 常山县| 雷州市| 长葛市| 忻城县| 栾城县| 武川县| 汉沽区| 吉木乃县| 天气| 平塘县| 庆安县| 蕉岭县| 新密市| 茌平县| 舒城县| 收藏| 甘孜县|