public class CommonClass
{
public CommonClass()
{
//
// TODO: Add constructor logic here
//
}
public string g_connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings ["Connection_conn"].ToString();
private int g_index;
private SqlConnection g_connecting;
//关闭连结
public void Connectionclose()
{
if (g_connecting.State == ConnectionState.Open)
{
g_connecting.Close();
g_connecting.Dispose();
}
}
//执行SQL语句
public string ExecuteSQL(string Actionquery)
{
SqlConnection g_connectiong = new SqlConnection(g_connectionstring);
SqlCommand command = new SqlCommand(Actionquery, g_connecting);
g_connecting.Open();
try
{
return command.ExecuteNonQuery().ToString();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (g_connecting.State == ConnectionState.Open)
{
g_connecting.Close();
}
}
}
//判断是否读取
public SqlDataReader GetDataRead(string sqlstring)
{
g_connecting = new SqlConnection(g_connectionstring);
SqlCommand command = new SqlCommand(sqlstring, g_connecting);
g_connecting.Open();
SqlDataReader DataRead;
try
{
DataRead = command.ExecuteReader(CommandBehavior.CloseConnection);
return DataRead;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
//返回DataView
public DataView GetDataView(string sqlstring)
{
DataView dataview;
SqlDataAdapter dataadapter = new SqlDataAdapter();
DataSet dataset = new DataSet();
string tablename;
g_connecting = new SqlConnection(g_connectionstring);
g_connecting.Open();
dataadapter = new SqlDataAdapter(sqlstring, g_connecting);
g_index += 1;
tablename = "Table" + g_index;
try
{
dataadapter.Fill(dataset, tablename);
dataview = new DataView(dataset.Tables[tablename]);
return dataview;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (g_connecting.State == ConnectionState.Open)
{
g_connecting.Close();
}
}
}
//返回dataset
public DataSet GetDataSet(string sqlstring)
{
SqlDataAdapter dataadapter = new SqlDataAdapter(sqlstring, g_connecting);
DataSet dataset = new DataSet();
try
{
dataadapter.Fill(dataset);
return dataset;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
//执行任意sql语句
public DataTable ExecQuerySql(string sqlstring)
{
try
{
g_connecting = new SqlConnection(g_connectionstring);
SqlCommand command = new SqlCommand(sqlstring, g_connecting);
command.CommandTimeout = 100000000;
SqlDataAdapter dataadapter = new SqlDataAdapter(command);
DataTable datatable = new DataTable();
dataadapter.Fill(datatable);
return datatable;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
//邦定Gridview
public void BindAnyGridView(string sqlstring, GridView gridview)
{
try
{
DataTable datatable = this.ExecQuerySql(sqlstring);
gridview.DataSource = datatable;
gridview.DataBind();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
//邦定DropDownList
public void BindAnyDropDownList(string sqlstring, DropDownList dropdownlist, string text, string value)
{
try
{
DataSet dataset = this.ExecQuerySqlDs(sqlstring);
dropdownlist.DataTextField = text;
dropdownlist.DataValueField = value;
dropdownlist.DataSource = dataset.Tables[0];
dropdownlist.DataBind();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
public DataSet ExecQuerySqlDs(string sqlstring)
{
try
{
g_connecting = new SqlConnection(g_connectionstring);
SqlCommand command = new SqlCommand(sqlstring, g_connecting);
command.CommandTimeout = 100000000;
SqlDataAdapter dataadapter = new SqlDataAdapter(command);
DataSet dataset = new DataSet();
dataadapter.Fill(dataset);
return dataset;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}