ASP.NET下使用ADO.NET访问本地数据库
来源:互联网 发布:路由器mac是什么 编辑:程序博客网 时间:2024/04/30 07:16
对于ASP.NET控件,相信大家都用的很舒服,但我感觉控件还是存在不少缺陷的,首先就是移植性不是很好,代码才是王道,下面给出ADO.NET代码,只要把这个文件添加到网站工程的app_code下,然后修改几个数值,你就能轻松访问自己本地的数据库了。 ADO.NET代码如下: using System; namespace WB /// <summary> #region 定义公共变量 //查看详情传值 public static SqlConnection con = new SqlConnection(sql_con); /// <summary> 添加好以后调用,需要在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)"; 感谢老大提供技术支持
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;
{
///数据库内部数据的访问
///该类中重要的变量已注明
/// </summary>
public class DataAccess
{
public DataAccess()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
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;
/// <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
///该方法用于执行赋值给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的时候就能看到,后面一个是你的数据库名称。
DataAccess.sqlDo();
- ASP.NET下使用ADO.NET访问本地数据库
- 使用ADO.NET访问数据库
- 使用ADO.NET访问数据库
- 使用ADO.NET访问数据库
- ADO.NET数据库访问
- ADO.NET数据库访问
- Ado.Net 访问数据库
- ADO.NET访问数据库
- ADO.NET使用存储过程访问数据库
- asp.net使用ado连接数据库
- asp.net中使用ado.net语言连接数据库
- 通过ADO.NET访问数据库
- ADO.NET数据库访问入门
- 通过ADO.NET访问数据库
- 数据库访问ADO.NET结构图
- ADO.NET数据库访问入门
- 数据库访问ADO.NET结构图
- ADO.NET数据库访问技术
- c 1-8
- 期待
- c 1-9
- C++特性 之 多态性
- 一只在IT线努力奋斗的小菜鸟(转)很感人
- ASP.NET下使用ADO.NET访问本地数据库
- 基于SOA的web service调用
- 据说是世界编程大赛第一名写的程序
- linux RPM包使用
- 【转】MySQL常用命令总结
- 触发器
- 种豆得豆
- 嵌入式学习,如何选择?
- 憋得慌啊!