📄 dataoperate.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// dataOperate 的摘要说明
/// </summary>
namespace myPaimai
{
public class dataOperate
{
public SqlParameterCollection spc;
private string conString ="Data Source=.;Initial Catalog=onlinePaimai;Integrated Security=True";
private SqlConnection conn = new SqlConnection();
private SqlCommand cmd = new SqlCommand();
public dataOperate(string conString)
{
this.conString = conString;
conn.ConnectionString = this.conString;
cmd.Connection = conn;
}
public dataOperate()
{
conn.ConnectionString = this.conString;
cmd.Connection = conn;
spc = cmd.Parameters;
}
private int connectDatabase()//打开数据库
{
try
{
if(conn.State==ConnectionState.Closed)
{
conn.Open();
}
return 0;
}
catch(Exception e)
{
HttpContext.Current.Response.Write("connectDatabase failed..."+e.Message);
return -1;
}
}
private int closeDatabase()//关闭数据库
{
try
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
return 0;
}
catch (Exception e)
{
HttpContext.Current.Response.Write("closeDatabase failed..." + e.Message);
return -1;
}
}
public object executeScalar(string sql)//返回首行首列
{
if (sql == "")
{
return null;
}
else
{
connectDatabase();
this.cmd.CommandText = sql;
this.cmd.CommandType = CommandType.Text;
try
{
return cmd.ExecuteScalar();
}
catch (Exception e)
{
HttpContext.Current.Response.Write("executeScalar failed..." + e.Message);
return null;
}
finally
{
this.closeDatabase();
}
}
}
public int executeNonQuery(string sql)//更改数据库
{
if (sql == "")
{
return -1;
}
else
{
connectDatabase();
this.cmd.CommandType = CommandType.Text;
this.cmd.CommandText = sql;
try
{
cmd.ExecuteNonQuery();
return 0;
}
catch (Exception e)
{
HttpContext.Current.Response.Write("executeNonQuery failed..." + e.Message);
return -1;
}
finally
{
this.closeDatabase();
}
}
}
public DataView getDataView(string sql)
{
if (sql == "")
{
return null;
}
else
{
try
{
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(this.cmd);
connectDatabase();
this.cmd.CommandType = CommandType.Text;
this.cmd.CommandText = sql;
sda.Fill(ds);
return ds.Tables[0].DefaultView ;
}
catch (Exception e)
{
HttpContext.Current.Response.Write("getDataView failed..." + e.Message);
return null;
}
finally
{
this.closeDatabase();
}
}
}
public int setParameter(string name,object value)//设置参数
{
if (name != "" && (value != null))
{
cmd.Parameters.Add(new SqlParameter(name, value));
return 0;
}
else
{
return -1;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -