C# WebFrom 链接数据库

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();
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值