|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace test2
{
class DataBase
{
public static String sqlconn = @"Data Source=LXL;Initial Catalog=Stu;User ID=sa;password=sa";
private SqlConnection conn;
public void open()
{
if (this.conn == null)
{
this.conn = new SqlConnection(sqlconn);
}
this.conn.Open();
}
public void close()
{
if (this.conn != null)
{
this.conn.Close();
}
}
public DataView runSelectSQL(String sql)
{
this.open();
SqlDataAdapter sda = new SqlDataAdapter(sql, this.conn);
DataSet ds = new DataSet();
sda.Fill(ds);
this.close();
return ds.Tables[0].DefaultView;
}
public DataSet getResultToDataSet(String sql)
{
this.open();
SqlDataAdapter sda = new SqlDataAdapter(sql, this.conn);
DataSet ds = new DataSet();
sda.Fill(ds);
this.close();
return ds;
}
//new DataBase().FillCb("sexTable","sex","id",cmbSex);
public void FillCb(string sTableName,
string sDisplayMember,
string sValueMember,
ComboBox cb)
{
DataView dv = new DataView();
string sSql = "select distinct "
+ sDisplayMember + ","
+ sValueMember +
" from " + sTableName;
dv = this.runSelectSQL(sSql);
cb.DataSource = dv;
cb.DisplayMember = sDisplayMember;
cb.ValueMember = sValueMember;
}
public int runUpdateSQL(String sql)
{
this.open();
SqlCommand cmd = new SqlCommand(sql, conn);
int n = cmd.ExecuteNonQuery();
this.close();
return n;
}
public int getRecordCount(string sSQLString)
{
int nCount;
this.open();
SqlCommand SqlComm;
try
{
SqlComm = new SqlCommand(sSQLString, this.conn);
nCount = Convert.ToInt32(SqlComm.ExecuteScalar());
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
nCount = -1;
}
this.close();
return nCount;
}
}
} |
|