新聞中心
使用 .Net 實(shí)現(xiàn)數(shù)據(jù)庫(kù)連接的方法

專注于為中小企業(yè)提供網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)黑山免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了超過(guò)千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
隨著互聯(lián)網(wǎng)的發(fā)展,數(shù)據(jù)庫(kù)作為信息系統(tǒng)中不可或缺的一部分,扮演著越來(lái)越重要的角色。在數(shù)據(jù)庫(kù)開(kāi)發(fā)中,連接數(shù)據(jù)庫(kù)是最基礎(chǔ)的操作之一。本文將介紹使用 .Net 實(shí)現(xiàn)數(shù)據(jù)庫(kù)連接的方法,以幫助讀者在開(kāi)發(fā)中更高效地連接數(shù)據(jù)庫(kù)。
一、連接方式
在連接方式上,我們有兩種選擇:一種是數(shù)據(jù)庫(kù)本地連接,一種是遠(yuǎn)程連接。數(shù)據(jù)庫(kù)本地連接是指連接同一臺(tái)計(jì)算機(jī)上的數(shù)據(jù)庫(kù),而遠(yuǎn)程連接是指連接另一臺(tái)計(jì)算機(jī)上的數(shù)據(jù)庫(kù)。在實(shí)際開(kāi)發(fā)中,一般都是使用遠(yuǎn)程連接。
二、什么是 .Net?
.Net 是一個(gè)由微軟公司開(kāi)發(fā)的開(kāi)放源代碼框架。它可以讓開(kāi)發(fā)人員使用不同的編程語(yǔ)言來(lái)開(kāi)發(fā) Windows 應(yīng)用程序。.Net 框架中包括了一個(gè)龐大的類庫(kù),以及形如 C#、VB.Net 和 F# 等多種編程語(yǔ)言。
三、.Net 連接數(shù)據(jù)庫(kù)方法
在 .Net 中,可以使用 ADO.NET 來(lái)連接數(shù)據(jù)庫(kù)。ADO.NET 是一個(gè)由 Microsoft 公司開(kāi)發(fā)的框架,用于操作數(shù)據(jù)源(如數(shù)據(jù)庫(kù))。ADO.NET 提供了一組數(shù)據(jù)訪問(wèn)服務(wù),包括連接到數(shù)據(jù)庫(kù)、執(zhí)行命令、讀寫數(shù)據(jù)、執(zhí)行事務(wù)等。下面我們就來(lái)介紹一下連接數(shù)據(jù)庫(kù)的具體實(shí)現(xiàn)方法。
1. 使用 System.Data.SqlClient 類連接 SQL Server 數(shù)據(jù)庫(kù)
SqlConnection 是一個(gè)由 System.Data.SqlClient 命名空間提供的類,它用于連接到 SQL Server 數(shù)據(jù)庫(kù)。使用 SqlConnection 連接數(shù)據(jù)庫(kù)時(shí)需要連接字符串(connection string)。連接字符串是一個(gè)包含連接參數(shù)的字符串,它告訴 SqlConnection 如何連接到數(shù)據(jù)庫(kù)。下面是一個(gè)連接字符串的示例:
string connectionString = @”Data Source=.\SQLEXPRESS;Initial Catalog=myDatabase;Integrated Security=True”;
其中,“Data Source”表示數(shù)據(jù)源,即 SQL Server 數(shù)據(jù)庫(kù)服務(wù)器的名稱或 IP 地址?!?\SQLEXPRESS”代表本地SQL Server Express實(shí)例;“Initial Catalog”是數(shù)據(jù)庫(kù)名稱;“Integrated Security”表示使用 Windows 身份驗(yàn)證。
下面是一個(gè) SqlConnection 的示例代碼:
using System.Data.SqlClient; // 引用 System.Data.SqlClient 命名空間
string connectionString = @”Data Source=.\SQLEXPRESS;Initial Catalog=myDatabase;Integrated Security=True”;
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
// do something …
conn.Close();
調(diào)用 SqlConnection 對(duì)象的 Open 方法會(huì)打開(kāi)與 SQL Server 數(shù)據(jù)庫(kù)的連接。當(dāng)不再需要連接時(shí),調(diào)用 SqlConnection 對(duì)象的 Close 方法可以關(guān)閉連接。需要注意的是,在使用 SqlConnection 連接 SQL Server 數(shù)據(jù)庫(kù)時(shí),必須保證 SQL Server 服務(wù)正常運(yùn)行。
2. 使用 OdbcConnection 類連接其他類型的數(shù)據(jù)庫(kù)
SqlConnection 類只能用來(lái)連接 SQL Server 數(shù)據(jù)庫(kù),而我們?cè)趯?shí)際開(kāi)發(fā)中可能會(huì)涉及到其他類型的數(shù)據(jù)庫(kù),如 Oracle、MySQL 等。這時(shí)候可以使用 OdbcConnection 類來(lái)連接數(shù)據(jù)庫(kù)。OdbcConnection 是一個(gè)由 System.Data.Odbc 命名空間提供的類,它允許開(kāi)發(fā)人員使用 ODBC(開(kāi)放式數(shù)據(jù)庫(kù)連接)協(xié)議訪問(wèn)數(shù)據(jù)庫(kù)。
下面是使用 OdbcConnection 連接 Oracle 數(shù)據(jù)庫(kù)的示例代碼:
using System.Data.Odbc; // 引用System.Data.Odbc命名空間
string connectionString = “Driver={Microsoft ODBC for Oracle};Server=myServerAddress;Uid=myUsername;Pwd=myPassword;”;
OdbcConnection conn = new OdbcConnection(connectionString);
conn.Open();
// do something …
conn.Close();
需要注意的是,連接字符串的格式可能會(huì)因數(shù)據(jù)庫(kù)類型的不同而有所不同。開(kāi)發(fā)人員可以查閱相應(yīng)數(shù)據(jù)庫(kù)的連接字符串格式以獲取正確的格式。
四、
本文針對(duì) .Net 連接數(shù)據(jù)庫(kù)的操作進(jìn)行了簡(jiǎn)單的介紹。需要注意的是,在實(shí)際開(kāi)發(fā)中,我們需要根據(jù)實(shí)際情況選擇合適的連接方式和數(shù)據(jù)訪問(wèn)類。通過(guò)本文的介紹,讀者可以更好地了解 .Net 下的數(shù)據(jù)庫(kù)連接方式,為實(shí)際開(kāi)發(fā)提供參考。
相關(guān)問(wèn)題拓展閱讀:
- ASP.NET三層架構(gòu)DAL層連接數(shù)據(jù)庫(kù)的方法
ASP.NET三層架構(gòu)DAL層連接數(shù)據(jù)庫(kù)的方法
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace StuDAL
{
public static class DBHelper
{
private static SqlConnection connection;
public static SqlConnection Connection
{
get
{
string connectionString = ConfigurationManager.ConnectionStrings.ConnectionString;
connection = new SqlConnection(connectionString);
if (connection == null)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
public static int ExecuteCommand(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
int result = cmd.ExecuteNonQuery();
return result;
}
public static int ExecuteCommand(string sql, params SqlParameter values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
return cmd.ExecuteNonQuery();
}
public static string ReturnStringScalar(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
try
{
string result = cmd.ExecuteScalar().ToString();
return result;
}
catch (Exception e)
{
return “0”;
}
connection.Close();
}
public static int GetScalar(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
try
{
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
catch (Exception e)
{
return 0;
}
connection.Close();
}
public static int GetScalar(string sql, params SqlParameter values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
connection.Close();
}
public static SqlDataReader GetReader(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
reader.Close();
reader.Dispose();
}
public static SqlDataReader GetReader(string sql, params SqlParameter values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
reader.Close();
reader.Dispose();
}
public static DataTable GetDataSet(string safeSql)
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(safeSql, Connection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
connection.Close();
connection.Dispose();
return ds.Tables;
}
public static DataTable GetDataSet(string sql, params SqlParameter values)
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
connection.Close();
connection.Dispose();
return ds.Tables;
}
}
}
假設(shè)數(shù)據(jù)庫(kù)連接字符串在web.config里配置如下慧晌巖
對(duì)你的DAL項(xiàng)目右鍵添加System.Configuration引用(必須步驟),
添加Model項(xiàng)目引用
然后在DB類(假設(shè)為UserDAL.cs)
using System.Configuration;//這個(gè)必須.
using Model那個(gè)項(xiàng)目
public class UserDAL
{
public const string ConnectionString = ConfigurationManager.ConnectionStrings .ConnectionString;
public int Insert(UserInfo user)/謹(jǐn)碰/ Model
{
SqlConnection sqlcon=new SqlConnection ( ConnectionString );
……………..
}
}
BLL
添加DB和Model項(xiàng)目引用
using 那前御個(gè)DB
public class UserBLL
{
public int Insert(UserInfo user)
{
int i = UserDAL.Insert(user);
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace SQLServerDAL
{
///
/// 數(shù)據(jù)操作類DBHelper.cs
/// 創(chuàng)建于:31-09
/// 作者: Administrator
/宏仿扒//大消 蔽昌
class DBHelper
{
static SqlConnection cnn;
static string cnnString ;
static DBHelper()
{
cnnString= ConfigurationManager.ConnectionStrings.ConnectionString;
}
public static SqlConnection Connection
{
get
{
if(cnn==null)
{
cnn = new SqlConnection(cnnString);
}
if (cnn.State==ConnectionState.Closed)
{
cnn.Open();
}
if (cnn.State==ConnectionState.Broken)
{
cnn.Close();
cnn.Open();
}
return cnn;
}
}
public static int ExecuteCommand(string sql)
{
SqlCommand cmd = Connection.CreateCommand();
cmd.CommandText = sql;
int x = cmd.ExecuteNonQuery();
Connection.Close();
return x;
}
public static int ExecuteCommand(string procName,SqlParameter ps)
{
SqlCommand cmd = Connection.CreateCommand();
cmd.CommandText = procName;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(ps);
int x = cmd.ExecuteNonQuery();
Connection.Close();
return x;
}
public static DataTable GetTable(string sql)
{
SqlCommand cmd = Connection.CreateCommand();
cmd.CommandText = sql;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public static DataTable GetTable(string procName, SqlParameter ps)
{
SqlCommand cmd = Connection.CreateCommand();
cmd.CommandText = procName;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(ps);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public static object GetScalar(string sql)
{
SqlCommand cmd = Connection.CreateCommand();
cmd.CommandText = sql;
object x = cmd.ExecuteScalar();
Connection.Close();
return x;
}
public static object GetScalar(string procName, SqlParameter ps)
{
SqlCommand cmd = Connection.CreateCommand();
cmd.CommandText = procName;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(ps);
object x = cmd.ExecuteScalar();
Connection.Close();
return x;
}
}
}
using System.Data.SqlClient;
using System.Text;
public int Add(Model.User model)
{
StringBuilder strSql = new StringBuilder();
strSql.Append(“insert into User(“);
strSql.Append(“Name,pwd,Email”);
strSql.Append(“)”);
strSql.Append(” values (“);
strSql.Append(“‘” + model.TxtName+ “‘旁纖,”);
strSql.Append(“‘” + model.txtPwd+ “‘,”);
strSql.Append(“運(yùn)搜仿'” + model.txtEmail+ “‘”);
strSql.Append(“)”);
return SQLHelper.getInt(strSql.ToString());
}
SQLHelper.cs頁(yè)面
using System.Data.SqlClient;
public class SQLHelper
{
protected static SqlConnection connection = new SqlConnection(“server=.;database=db;integrated security=true”漏櫻);
public int getInt(string sql)
{
try
{
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
SqlCommand command = new SqlCommand(sql, connection);
int Int = Convert.ToInt32(command.ExecuteNonQuery());
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
return Int;
}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
}
}
}
.net連接數(shù)據(jù)庫(kù)的方式的介紹就聊到這里吧,感謝你花時(shí)間閱讀本站內(nèi)容,更多關(guān)于.net連接數(shù)據(jù)庫(kù)的方式,使用.Net實(shí)現(xiàn)數(shù)據(jù)庫(kù)連接的方法,ASP.NET三層架構(gòu)DAL層連接數(shù)據(jù)庫(kù)的方法的信息別忘了在本站進(jìn)行查找喔。
香港服務(wù)器選創(chuàng)新互聯(lián),2H2G首月10元開(kāi)通。
創(chuàng)新互聯(lián)(www.cdcxhl.com)互聯(lián)網(wǎng)服務(wù)提供商,擁有超過(guò)10年的服務(wù)器租用、服務(wù)器托管、云服務(wù)器、虛擬主機(jī)、網(wǎng)站系統(tǒng)開(kāi)發(fā)經(jīng)驗(yàn)。專業(yè)提供云主機(jī)、虛擬主機(jī)、域名注冊(cè)、VPS主機(jī)、云服務(wù)器、香港云服務(wù)器、免備案服務(wù)器等。
網(wǎng)站欄目:使用.Net實(shí)現(xiàn)數(shù)據(jù)庫(kù)連接的方法(.net連接數(shù)據(jù)庫(kù)的方式)
標(biāo)題URL:http://m.fisionsoft.com.cn/article/codecdd.html


咨詢
建站咨詢
