public SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);
public SqlCommand cmd = new SqlCommand();
public SqlDataAdapter objadpt = new SqlDataAdapter();
public String sqlStr;
DataTable dt = new DataTable();
public basedata()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 根据sql语句返回查询结果
/// </summary>
/// <param name="sqlStr"></param>
/// <returns></returns>
public DataTable gettable(string sqlStr)
{
if(conn .State== ConnectionState.Closed)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = sqlStr;
objadpt.SelectCommand = cmd;
dt.Clear();
objadpt.Fill(dt);
if (conn.State == ConnectionState.Open)
conn.Close();
return dt;
}
/// <summary>
/// 执行查询语句,返回SqlDataReader
/// </summary>
/// <param name="strSQL">查询语句</param>
/// <returns>SqlDataReader</returns>
public SqlDataReader GetReader(string sqlStr)
{
if (conn.State == ConnectionState.Closed)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = sqlStr;
SqlDataReader myReader = cmd.ExecuteReader();
return myReader;
if (conn.State == ConnectionState.Open)
conn.Close();
}
/// <summary>
/// 执行sql语句,如delete或update
/// </summary>
/// <param name="sqlStr"></param>
public void executecmd(string sqlStr)
{
if (conn.State == ConnectionState.Closed)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = sqlStr;
cmd.ExecuteNonQuery();
if (conn.State == ConnectionState.Open)
conn.Close();
}
public string executecmdandback(string sqlStr)
{
string id;
if (conn.State == ConnectionState.Closed)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = sqlStr + ";SELECT SCOPE_IDENTITY()";
id = cmd.ExecuteScalar().ToString();//添加配偶并返回刚添加的ID
if (conn.State == ConnectionState.Open)
conn.Close();
return id;
}
/// <summary>
/// 按条件返回查询结果
/// </summary>
/// <param name="sqlStr"></param>
/// <returns></returns>
public string getresult(string sqlStr)
{
string id;
if (conn.State == ConnectionState.Closed)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = sqlStr;
id = Convert.ToString(cmd.ExecuteScalar());
if (conn.State == ConnectionState.Open)
conn.Close();
return id;
}
public SqlCommand cmd = new SqlCommand();
public SqlDataAdapter objadpt = new SqlDataAdapter();
public String sqlStr;
DataTable dt = new DataTable();
public basedata()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 根据sql语句返回查询结果
/// </summary>
/// <param name="sqlStr"></param>
/// <returns></returns>
public DataTable gettable(string sqlStr)
{
if(conn .State== ConnectionState.Closed)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = sqlStr;
objadpt.SelectCommand = cmd;
dt.Clear();
objadpt.Fill(dt);
if (conn.State == ConnectionState.Open)
conn.Close();
return dt;
}
/// <summary>
/// 执行查询语句,返回SqlDataReader
/// </summary>
/// <param name="strSQL">查询语句</param>
/// <returns>SqlDataReader</returns>
public SqlDataReader GetReader(string sqlStr)
{
if (conn.State == ConnectionState.Closed)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = sqlStr;
SqlDataReader myReader = cmd.ExecuteReader();
return myReader;
if (conn.State == ConnectionState.Open)
conn.Close();
}
/// <summary>
/// 执行sql语句,如delete或update
/// </summary>
/// <param name="sqlStr"></param>
public void executecmd(string sqlStr)
{
if (conn.State == ConnectionState.Closed)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = sqlStr;
cmd.ExecuteNonQuery();
if (conn.State == ConnectionState.Open)
conn.Close();
}
public string executecmdandback(string sqlStr)
{
string id;
if (conn.State == ConnectionState.Closed)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = sqlStr + ";SELECT SCOPE_IDENTITY()";
id = cmd.ExecuteScalar().ToString();//添加配偶并返回刚添加的ID
if (conn.State == ConnectionState.Open)
conn.Close();
return id;
}
/// <summary>
/// 按条件返回查询结果
/// </summary>
/// <param name="sqlStr"></param>
/// <returns></returns>
public string getresult(string sqlStr)
{
string id;
if (conn.State == ConnectionState.Closed)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = sqlStr;
id = Convert.ToString(cmd.ExecuteScalar());
if (conn.State == ConnectionState.Open)
conn.Close();
return id;
}