连接数据库的字符串

<1>

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
	<add name="ConnStr" connectionString="server=.;database=sales;uid=sa;pwd=123456;" providerName="System.Data.SqlClient" />
    <add name="ConnStr2" connectionString="data source=FFB-VAIO;initial catalog=sales;integrated security=true"/>
  </connectionStrings>
</configuration>

连接数据库的两种写法

<1>

SqlConnection m_SqlConnection; = new SqlConnection(System.Web.Compilation.ConnectionStringsExpressionBuilder
	.GetConnectionString("getConn"));

<2>

string connStr = ConfigurationManager.ConnectionStrings["getConn"].ConnectionString;
SqlConnection m_SqlConnection = new SqlConnection(connStr);

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text.RegularExpressions;

namespace ETong.ETM.WCF.Service
{
    /// <summary>
    /// general data access class
    /// </summary>
    internal class SmartDataProvider
    {
        /// <summary>
        /// SqlConnection object
        /// </summary>
        SqlConnection m_SqlConnection;
      
        /// <summary>
        /// initialize the component
        /// </summary>
        internal SmartDataProvider()
        {
            m_SqlConnection = new SqlConnection(System.Web.Compilation.ConnectionStringsExpressionBuilder.GetConnectionString("SQLConnectionString"));
        }

        /// <summary>
        /// initialize the component
        /// </summary>
        internal SmartDataProvider(string sqlConnectionString)
        {
            m_SqlConnection = new SqlConnection(sqlConnectionString);
        }

        /// <summary>
        /// Get data set object
        /// </summary>
        /// <param name="sqlQuery">SQL select command text</param>
        /// <returns>DataSet</returns>
        internal DataSet GetData(string sqlQuery)
        {
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlQuery, m_SqlConnection);
            DataSet ds = new DataSet();
            sqlDataAdapter.Fill(ds);
            return ds;
        }

        /// <summary>
        /// Get dataset object from database
        /// </summary>
        /// <param name="sqlArray">sql command text array</param>
        /// <returns>DataSet</returns>
        internal DataSet GetDataSet(string[] sqlArray)
        {
            DataSet dataSet = new DataSet();
            try
            {
                foreach (string sql in sqlArray)
                {
                    DataTable dt = new DataTable();
                    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, m_SqlConnection);
                    sqlDataAdapter.FillSchema(dt, SchemaType.Source);
                    sqlDataAdapter.Fill(dt);
                    dataSet.Tables.Add(dt);
                }
            }
            catch (System.Data.SqlClient.SqlException Err)
            {
                m_SqlConnection.Close();
                throw Err;
            }
            finally
            {
                m_SqlConnection.Close();
            }
            return dataSet;
        }
      
        /// <summary>
        /// Get datatable object from database
        /// </summary>
        /// <param name="sqlText">sql command text</param>
        /// <param name="containsDataSchema">contains Data Schema tag</param>
        /// <returns>DataTable</returns>
        internal DataTable GetDataTable(string sqlText, bool containsDataSchema)
        {
            try
            {
                DataTable dt = new DataTable();
                SqlDataAdapter adapter = new SqlDataAdapter(sqlText, m_SqlConnection);
                if (containsDataSchema)
                    adapter.FillSchema(dt, SchemaType.Source);
                adapter.Fill(dt);
                if (dt.TableName.Trim().Length==0)
                    dt.TableName = Guid.NewGuid().ToString();
                return dt;
            }
            catch (System.Data.SqlClient.SqlException Err)
            {
                throw Err;
            }
            finally
            {
                m_SqlConnection.Close();
            }
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值