怎么在C#中使用SqlConnection連接SQL Server?針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
(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身份驗證 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身份驗證 //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ù)器的會話。
/// <summary> /// 嘗試連接數(shù)據(jù)庫 /// </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()方法終止會話
private bool DisConnect() { if (m_SqlConnection == null) return true; try { m_SqlConnection.Close(); } catch (Exception e) { Debug.WriteLine(e.Message); return false; } return true;
許多程序員都使連接一直處于打開狀態(tài),直到程序結(jié)束為止,這通常會浪費服務(wù)器資源。與這種打開一次,永不關(guān)閉的方式相比,使用連接池,在需要時打開和關(guān)閉連接要更加高效。
如下所示,我們封裝一個執(zhí)行SQL存儲過程的函數(shù):
/// <summary> /// 執(zhí)行返回查詢結(jié)果的存儲過程 /// </summary> /// <param name="procname">存儲過程名?</param> /// <param name="param">參數(shù)。函數(shù)正常返回時,所有類型為out的參數(shù)值也在對應(yīng)位置上</param> /// <param name="result">返回查詢的結(jié)果</param> /// <returns>0正確,其他錯誤</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ān)于怎么在C#中使用SqlConnection連接SQL Server問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計公司行業(yè)資訊頻道了解更多相關(guān)知識。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
文章題目:怎么在C#中使用SqlConnection連接SQLServer-創(chuàng)新互聯(lián)
網(wǎng)頁路徑:http://www.rwnh.cn/article22/doedjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、靜態(tài)網(wǎng)站、定制開發(fā)、外貿(mào)建站、全網(wǎng)營銷推廣、移動網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容