C#使用SqlConnection連接到SQL Server的代碼示例
使用SqlConnection連接到SQL Server 2012
示例如下:
(1). 利用SqlConnection創(chuàng)建連接
public SQLServerAPI(string str_ip, string str_db, string str_user, string str_pwd)
{
m_strIp = str_ip;
m_strDb = str_db;
m_strUser = str_user;
m_strPwd = str_pwd;
//SQLServer身份驗(yàn)證
m_strConnection = @"Data Source=" + m_strIp;
m_strConnection += @";Initial Catalog=" + m_strDb;
m_strConnection += @";UID=" + m_strUser + ";PWD=" + m_strPwd;
m_strConnection += ";Connection Timeout=10;Pooling=true;Max Pool Size=100";
//Windows身份驗(yàn)證
//m_strConnection =
@"server=localhost\SQLEXPRESS;database=SQL2012Db;Trusted_Connection=SSPI;";
DisConnect();
m_Transaction = null;
m_SqlConnection = new SqlConnection(m_strConnection);
}
(2). 調(diào)用Open方法,以建立與服務(wù)器的會(huì)話。
/// <summary>
/// 嘗試連接數(shù)據(jù)庫(kù)
/// </summary>
private bool Connect()
{
if (m_SqlConnection == null)
return false;
try
{
m_SqlConnection.Open();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
return false;
}
return true;
}
(3). 調(diào)用Close()方法終止會(huì)話
private bool DisConnect()
{
if (m_SqlConnection == null)
return true;
try
{
m_SqlConnection.Close();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
return false;
}
return true;
許多程序員都使連接一直處于打開(kāi)狀態(tài),直到程序結(jié)束為止,這通常會(huì)浪費(fèi)服務(wù)器資源。與這種打開(kāi)一次,永不關(guān)閉的方式相比,使用連接池,在需要時(shí)打開(kāi)和關(guān)閉連接要更加高效。
如下所示,我們封裝一個(gè)執(zhí)行SQL存儲(chǔ)過(guò)程的函數(shù):
/// <summary>
/// 執(zhí)行返回查詢結(jié)果的存儲(chǔ)過(guò)程
/// </summary>
/// <param name="procname">存儲(chǔ)過(guò)程名?</param>
/// <param name="param">參數(shù)。函數(shù)正常返回時(shí),所有類型為out的參數(shù)值也在對(duì)應(yīng)位置上</param>
/// <param name="result">返回查詢的結(jié)果</param>
/// <returns>0正確,其他錯(cuò)誤</returns>
public int ExecQueryStoreProc(string procname, ref SqlParameter[] param, out DataTable result)
{
if (!Connect())
{
result = null;
return -1;
}
try
{
SqlCommand command = new SqlCommand(procname, m_SqlConnection);
command.CommandType = CommandType.StoredProcedure;
if (m_Transaction != null)
command.Transaction = m_Transaction;
SqlParameter rvalue = command.Parameters.Add(new SqlParameter("RETURN_VALUE", SqlDbType.Int));
rvalue.Direction = ParameterDirection.ReturnValue;
if (param != null)
command.Parameters.AddRange(param);
result = new DataTable();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
result.Load(reader);
return Convert.ToInt32(command.Parameters["RETURN_VALUE"].Value);
}
catch (Exception)
{
result = null;
return -1;
}
finally
{
DisConnect();
}
}
上述過(guò)程就是在需要時(shí)打開(kāi)和關(guān)閉連接的實(shí)現(xiàn)方式,另外finally塊始終調(diào)用Close()方法,這并不會(huì)造成問(wèn)題或者過(guò)多地浪費(fèi)資源,而且能確保關(guān)閉連接。
以上所述是小編給大家介紹的SQL Server創(chuàng)建連接代碼示例詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- c#連接sqlserver數(shù)據(jù)庫(kù)、插入數(shù)據(jù)、從數(shù)據(jù)庫(kù)獲取時(shí)間示例
- C#實(shí)現(xiàn)連接SQL Server2012數(shù)據(jù)庫(kù)并執(zhí)行SQL語(yǔ)句的方法
- C#編程實(shí)現(xiàn)連接SQL SERVER數(shù)據(jù)庫(kù)實(shí)例詳解
- C#連接到sql server2008數(shù)據(jù)庫(kù)的實(shí)例代碼
- C#連接SQL Server的實(shí)現(xiàn)方法
- C#實(shí)現(xiàn)異步連接Sql Server數(shù)據(jù)庫(kù)的方法
- 關(guān)于C#連接SQL Server時(shí)提示用戶登錄失敗的解決方法
- C#連接SQL Server數(shù)據(jù)庫(kù)的實(shí)例講解
- 使用C#連接SQL?Server的詳細(xì)圖文教程
- C#連接SQL?Sever數(shù)據(jù)庫(kù)詳細(xì)圖文教程
相關(guān)文章
在C#中使用二叉樹(shù)實(shí)時(shí)計(jì)算海量用戶積分排名的實(shí)現(xiàn)詳解
這篇文章主要介紹了在C#中使用二叉樹(shù)實(shí)時(shí)計(jì)算海量用戶積分排名的實(shí)現(xiàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
WPF實(shí)現(xiàn)帶模糊搜索的DataGrid的示例代碼
這篇文章主要為大家詳細(xì)介紹了WPF如何實(shí)現(xiàn)帶模糊搜索的DataGrid,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2023-02-02

