dbAccess.cs Text
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace ADO.W1
{
public partial class dbAccess
{
private string connS = "";
public dbAccess()
{
connS = System.Configuration.ConfigurationManager.ConnectionStrings["Test"].ToString();
}
// UPDATE
public bool UPDATE(string sql)
{
SqlConnection conn = new SqlConnection(connS);
SqlCommand cmd = new SqlCommand(sql,conn);
try
{
conn.Open();
cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
finally
{
conn.Close();
cmd.Dispose();
conn.Dispose();
}
}
// SELECT
public DataTable SELECT(string sql)
{
SqlDataAdapter da = new SqlDataAdapter(sql,connS);
DataTable dt = new DataTable("info");
try
{
da.Fill(dt);
int r = dt.Rows.Count;
return dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
finally
{
da.Dispose();
dt.Dispose();
}
}
//SELECT FROM READER
public DataTable SELECT_READER(string sql) {
SqlConnection conn = new SqlConnection(connS);
SqlCommand cmd = new SqlCommand(sql, conn);
DataTable dt = new DataTable("info");
SqlDataReader rd = null;
try
{
conn.Open();
rd = cmd.ExecuteReader();
dt = GetReaderTable(rd.GetSchemaTable());
object[] array = new object[dt.Columns.Count];
while (rd.Read())
{
for (int i = 0; i < dt.Columns.Count; i++)
{
array[i] = rd.GetValue(i);
}
dt.Rows.Add(array);
}
return dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
finally {
conn.Close();
rd.Dispose();
conn.Dispose();
dt.Dispose();
cmd.Dispose();
}
}
private DataTable GetReaderTable(DataTable pdt) {
DataTable dt = new DataTable("info");
for (int i = 0; i < pdt.Rows.Count; i++)
{
dt.Columns.Add(pdt.Rows[i][0].ToString());
}
return dt;
}
//SELECT FROM REDER_REDER
public SqlDataReader SELECT_READER(string sql,bool mode)
{
SqlConnection conn = new SqlConnection(connS);
SqlCommand cmd = new SqlCommand (sql,conn);
try
{
conn.Open();
SqlDataReader rd = cmd.ExecuteReader();
int dep = rd.Depth;
return rd;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
finally {
conn.Close();
cmd.Dispose();
conn.Dispose();
}
}
}
}
No comments:
Post a Comment