<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();
}
}