欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

C#使用SqlConnection連接到SQL Server的代碼示例

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次

使用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身份驗證
 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)閉連接的實現(xiàn)方式,另外finally塊始終調(diào)用Close()方法,這并不會造成問題或者過多地浪費資源,而且能確保關(guān)閉連接。

以上所述是小編給大家介紹的SQL Server創(chuàng)建連接代碼示例詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對我們網(wǎng)站的支持!

上一篇:C# WPF上位機實現(xiàn)和下位機TCP通訊的方法

欄    目:C#教程

下一篇:C#用記事本編寫簡單WinForm窗體程序

本文標(biāo)題:C#使用SqlConnection連接到SQL Server的代碼示例

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/4814.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有