| < add name = "TestConnectionString" connectionString = "Data Source=xxxx;Initial Catalog=DBName;User ID=sa;Password=test" |
| providerName = "System.Data.SqlClient" /> |
/*aspx.cs*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Diagnostics;
using System.IO;
/// <summary>
/// Run Sql
/// </summary>
public class Sql
{
public Sql()
{
//
// TODO: 在此加入建構函式的程式碼
//
}
private SqlConnection conn;
private string _ErrorMsg;
private DataSet _ds = new DataSet();
/// <summary>
/// Bind Server
/// </summary>
private void BindServer()
{
try
{
conn = new
SqlConnection(ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString);
conn.Open();
}
catch (Exception e)
{
_ErrorMsg = e.ToString();
}
}
/// <summary>
/// UnBind Server
/// </summary>
private void UnBindServer()
{
conn.Close();
}
/// <summary>
/// RunSqlQuery
/// </summary>
/// <param name="strSql">sql 字串</param>
/// <returns></returns>
public bool RunQuery(string strSql)
{
try
{
this.BindServer();
_ds.Reset();
//string strSql = "select * from NewCenter ";
SqlCommand cmd = new SqlCommand(strSql);
SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
sda.Fill(_ds);
this.UnBindServer();
return true;
}
catch (Exception e)
{
_ErrorMsg = e.ToString();
this.UnBindServer();
return false;
}
}
/// <summary>
/// RunNonQuery
/// </summary>
/// <param name="strSql">sql字串</param>
/// <returns></returns>
public bool RunNonQuery(string strSql,bool flag)
{
try
{
this.BindServer();
_ds.Reset();
SqlCommand cmd = new SqlCommand(strSql, conn);
cmd.ExecuteNonQuery();
conn.Close();
return true;
}
catch (Exception e)
{
_ErrorMsg = strSql+e.ToString();
conn.Close();
return false;
}
}
/// <summary>
/// 取得DataSet
/// </summary>
/// <returns></returns>
public DataSet GetDataTable()
{
return this._ds.Table[0];
}
/// <summary>
/// 取得錯誤訊息
/// </summary>
/// <returns>錯誤訊息</returns>
public string GetErrorMsg()
{
return _ErrorMsg;
}
}//end class
沒有留言:
張貼留言