web程序,会把类db.cs放在App_code文件夹中,代码如下:
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
/// <summary>
///db 的摘要说明
/// </summary>
public class db
{
public static string connectionstring = "server=localhost;database=db;uid=sa;pwd=password";
public db()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public static SqlConnection OpenDB()
{
try
{
SqlConnection oConn = new SqlConnection(connectionstring);
oConn.Open();
return oConn;
}
catch
{
throw;
}
}
public static void CloseDB(SqlConnection oConn)
{
try
{
oConn.Close();
}
catch
{
throw;
}
}
public static DataSet Execute(string strCommandString)
{
try
{
SqlConnection oConn = OpenDB();
DataSet oDataSet = new DataSet();
SqlDataAdapter oDataAdapter = new SqlDataAdapter(strCommandString, oConn);
oDataAdapter.Fill(oDataSet);
CloseDB(oConn);
return oDataSet;
}
catch
{
throw;
}
}
public static int ExecuteNonQuery(string strCommandString)
{
int li_count = 0;
SqlConnection oConn = null;
SqlCommand oComm = null;
try
{
oConn = new SqlConnection(connectionstring);
oComm = new SqlCommand();
oConn.Open();
oComm.Connection = oConn;
oComm.CommandText = strCommandString;
li_count = oComm.ExecuteNonQuery();
return li_count;
}
catch
{
return 0;
}
finally
{
if (oConn != null) oConn.Close();
}
}
public static int ExecuteReid(string strCommandString)
{
int li_count = 0;
SqlConnection oConn = null;
SqlCommand oComm = null;
try
{
oConn = new SqlConnection(connectionstring);
oComm = new SqlCommand();
oConn.Open();
oComm.Connection = oConn;
oComm.CommandText = strCommandString;
li_count = Convert.ToInt32(oComm.ExecuteScalar().ToString());
return li_count;
}
catch
{
return 0;
}
finally
{
if (oConn != null) oConn.Close();
}
}
}