欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

怎么在C#中使用SqlConnection連接SQLServer-創(chuàng)新互聯(lián)

怎么在C#中使用SqlConnection連接SQL Server?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

創(chuàng)新互聯(lián)專(zhuān)注于企業(yè)營(yíng)銷(xiāo)型網(wǎng)站、網(wǎng)站重做改版、晉中網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5網(wǎng)站設(shè)計(jì)成都商城網(wǎng)站開(kāi)發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性?xún)r(jià)比高,為晉中等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。

(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í)行返回查詢(xún)結(jié)果的存儲(chǔ)過(guò)程
/// </summary>
/// <param name="procname">存儲(chǔ)過(guò)程名?</param>
/// <param name="param">參數(shù)。函數(shù)正常返回時(shí),所有類(lèi)型為out的參數(shù)值也在對(duì)應(yīng)位置上</param>
/// <param name="result">返回查詢(xún)的結(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ān)于怎么在C#中使用SqlConnection連接SQL Server問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。

文章題目:怎么在C#中使用SqlConnection連接SQLServer-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)路徑:http://www.chinadenli.net/article22/doedjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)靜態(tài)網(wǎng)站定制開(kāi)發(fā)外貿(mào)建站全網(wǎng)營(yíng)銷(xiāo)推廣移動(dòng)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作