资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

怎么在C#中使用SqlConnection连接SQLServer-创新互联

怎么在C#中使用SqlConnection连接SQL Server?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

创新互联专注于企业营销型网站、网站重做改版、晋中网站定制设计、自适应品牌网站建设、H5网站设计成都商城网站开发、集团公司官网建设、成都外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为晋中等各大城市提供网站开发制作服务。

(1). 利用SqlConnection创建连接

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). 调用Open方法,以建立与服务器的会话。

/// 
/// 尝试连接数据库
/// 
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). 调用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;

许多程序员都使连接一直处于打开状态,直到程序结束为止,这通常会浪费服务器资源。与这种打开一次,永不关闭的方式相比,使用连接池,在需要时打开和关闭连接要更加高效。

如下所示,我们封装一个执行SQL存储过程的函数:

/// 
/// 执行返回查询结果的存储过程
/// 
/// 存储过程名?
/// 参数。函数正常返回时,所有类型为out的参数值也在对应位置上
/// 返回查询的结果
/// 0正确,其他错误
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();
	}
}

关于怎么在C#中使用SqlConnection连接SQL Server问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联成都网站设计公司行业资讯频道了解更多相关知识。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文题目:怎么在C#中使用SqlConnection连接SQLServer-创新互联
本文链接:http://cdkjz.cn/article/doedjc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220