ASP.NET下使用ADO.NET访问本地数据库

 对于ASP.NET控件,相信大家都用的很舒服,但我感觉控件还是存在不少缺陷的,首先就是移植性不是很好,代码才是王道,下面给出ADO.NET代码,只要把这个文件添加到网站工程的app_code下,然后修改几个数值,你就能轻松访问自己本地的数据库了。

ADO.NET代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace WB
{

    /// <summary>
    ///数据库内部数据的访问
    ///该类中重要的变量已注明
    /// </summary>
    public class DataAccess
    {


        public DataAccess()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        #region    定义公共变量
        public static string sql_con = "Data Source=FELVEN-PC//SQLEXPRESS;Initial Catalog=bbs;Integrated Security=True";   //连接字符串
        public static string UserID = "";          //考虑到用户登录后,要根据用户的ID来判断用户的身份,特设此字段以用来记录用户登录的ID
        public static string Key = "";
        public static string Date = "";
        public static string BDate = "";
        public static string EDate = "";
        public static int Result_Flag = 0;
        public static int Number = 0;


        //下面是获取模板页中的东西
        public static string Muser_ID = "";
        public static string Muser_Name = "";
        public static string Mdegree = "";
        public static string Mdepartment = "";

        //查看详情传值
        public static int Result_Number = 0;
        public static int Result_Whole = 0;

        public static SqlConnection con = new SqlConnection(sql_con);
        /// <summary>
        ///定义要执行的SQL语句
        /// </summary>
        public static string sqlcmd = "";          //定义要执行的SQL语句
        public static SqlCommand mycommand;
        public static SqlDataAdapter sda = new SqlDataAdapter();
        public static DataSet ds = new DataSet();
        #endregion

        /// <summary>
        ///该方法用于执行赋值给sqlcmd的SQL语句
        /// </summary>
        public static void sqlDo()
        {
            con.Close();
            if (con.State == ConnectionState.Closed)
                con.Open();
            mycommand = new SqlCommand(sqlcmd, con);
            if (sqlcmd.Substring(0, 1) == "s" || sqlcmd.Substring(0, 1) == "S")
                sda.SelectCommand = mycommand;
            else if (sqlcmd.Substring(0, 1) == "d" || sqlcmd.Substring(0, 1) == "D")
                sda.DeleteCommand = mycommand;
            else if (sqlcmd.Substring(0, 1) == "u" || sqlcmd.Substring(0, 1) == "U")
                sda.UpdateCommand = mycommand;
            else
                sda.InsertCommand = mycommand;
            mycommand.ExecuteNonQuery();
      //      SqlDataReader sdr = mycommand.ExecuteReader();
            if (sqlcmd.Substring(0, 1) == "s" || sqlcmd.Substring(0, 1) == "S")
            {
                ds.Tables.Clear();
                sda.Fill(ds);
            }
        }
    }
}
上面两处加红的,一个是自己本地数据库的地址,这个在你打开SQL server2005的时候就能看到,后面一个是你的数据库名称。

添加好以后调用,需要在cs文件中包含WB这个命名空间

using WB;

DataAccess.sqlcmd = "insert into whole(Patient_ID,Doctor_ID,Doing_Date,Doing_State) values ('" + dropPatient_ID.SelectedValue.Trim() + "','" + txtDoctor_ID.Text.Trim() + "','" + time + "',0)";
        DataAccess.sqlDo();

 

感谢老大提供技术支持

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页