基本SQL操作指令

摘要:基本SQL操作指令

using System.Configuration;
using System.Data.SqlClient;
using System.Data;

string strconn = ConfigurationManager.ConnectionStrings["LocDB"].ConnectionString;

//連線字串範例

//<add name="LocDB" connectionString="Data Source=localhost;Initial Catalog=North ;User Id=sa;Password=a12345;Max Pool Size = 1024;" //providerName="System.Data.SqlClient"/>

 

    #region <-SQL Select->
    private DataTable GetSelect(string _SQL)
    {
        SqlConnection cn = new SqlConnection(strconn); ;
        SqlDataAdapter dapt = new SqlDataAdapter();
        DataSet ds = new System.Data.DataSet();

        try
        {
            cn.Open();
            dapt = new SqlDataAdapter(_SQL, cn);
            dapt.Fill(ds);
            DataTable dt = ds.Tables[0];
            return dt;
        }
        catch
        {
            return null;
        }
        finally
        {
        dapt.Dispose();
        cn.Close();
        cn.Dispose();
        }

    }
    #endregion

    #region <-SQL NoSelect->
    private int GetNoSelect(string _SQL)
    {
        int row = -1;
        SqlConnection cn= new SqlConnection(strconn);
        SqlCommand cmd;

        try
        {
            cn.Open();
            cmd = new SqlCommand(_SQL, cn);
            row = cmd.ExecuteNonQuery();
            return row;
        }
        catch
        {
            return row;
        }
        finally
        {
        cn.Close();
        cn.Dispose();
        }

    }
    #endregion