爱问知识人 爱问教育 医院库

asp.net如何链接数据库

首页

asp.net如何链接数据库


        

提交回答
好评回答
  • 2010-09-27 15:27:00
    能,先建立数据库,后根据语句链接也可以 <appSettings> <add key="sqlserver" value="server=主机名;database=数据库名;uid=用户名;pwd=密码"/> </appSettings> 例如: <appSettings> <add key="sqlserver" value="server=(local);database=sqgl;uid=sa;pwd="/> </appSettings>

    p***

    2010-09-27 15:27:00

其他答案

    2010-09-28 15:48:00
  •  nfig中加上datdstring="serve=;uid=;pwd=;database=;"

    w***

    2010-09-28 15:48:00

  • 2010-09-27 13:56:00
  • 数据库连接字符串    "serve=服务器名;uid=用户名;pwd=密码;database=数据库名;"

    s***

    2010-09-27 13:56:00

  • 2010-09-27 13:30:00
  •   /// <summary>        /// 执行增删改        /// </summary>        /// <param name="sql">执行的SQL语句</param>        /// <returns>结果信息(成功:1; 失败:-1)</returns>        public int executeSQL(string sql)        {            int result = -1;            string connectInfo = "serve=。
      ;uid=sa;pwd=123456;database=test;"; using (SqlConnection con = new SqlConnection(connectInfo)) { try { SqlCommand com = new SqlCommand(sql, con); con。
      Open(); result = com。ExecuteNonQuery(); } catch (Exception ex) { throw new Exception(ex。
      Message); } finally { if (con != null) { if (con。
      State == ConnectionState。Open) { con。Close(); } } } } return result; }。
      

    l***

    2010-09-27 13:30:00

  • 2010-09-27 13:23:00
  •   连接access 首先看一个例子代码片断: 程序代码: using system。data; using system。data。oledb; 。。。。。。 string strconnection="provider=microsoft。jet。
      oledb。4。0;"; strconnection+=@"data source=c:\begaspnet\northwind。mdb"; oledbconnection objconnection=new oledbconnection(strconnection); 。
      。。。。。 objconnection。open(); objconnection。close(); 。。。。。。 解释: 连接access数据库需要导入额外的命名空间,所以有了最前面的两条using命令,这是必不可少的! strconnection这个变量里存放的是连接数据库所需要的连接字符串,他指定了要使用的数据提供者和要使用的数据源。
       "provider=microsoft。jet。oledb。4。0;"是指数据提供者,这里使用的是microsoft jet引擎,也就是access中的数据引擎,asp。net就是靠这个和access的数据库连接的。 "data source=c:\begaspnet\northwind。
      mdb"是指明数据源的位置,他的标准形式是"data source= \myfile。mdb"。 ps: 1。"+="后面的"@"符号是防止将后面字符串中的"\"解析为转义字符。 2。如果要连接的数据库文件和当前文件在同一个目录下,还可以使用如下的方法连接: strconnection+="data source="; strconnection+=mappath("northwind。
      
      mdb"); 这样就可以省得你写一大堆东西了! 3。要注意连接字符串中的参数之间要用分号来分隔。 "oledbconnection objconnection=new oledbconnection(strconnection);"这一句是利用定义好的连接字符串来建立了一个链接对象,以后对数据库的操作我们都要和这个对象打交道。

    s***

    2010-09-27 13:23:00

  • 2010-09-27 12:42:00
  • 很简单的。。键一个DB类(数据库操作类)所有的数据库操作都现在该类里面 在页面中调用就可以了。。。 而数据库链接字符串写一般写在Web.config文件中。。在数据库操作页面或其他需要的时候调用就可以了。。。

    x***

    2010-09-27 12:42:00

  • 2010-09-27 11:04:00
  •   <%@ Page Language="C#" ContentType="text/html" debug="true"%> <%@ Import Namespace="System。Data" %> <%@ Import Namespace="System。
      Data。OleDb"%> <%@ Register Tagprefix="tmenu" TagName="menu" Src="topmenu。ascx"%> <script language="c#" runat="server"> string value1,value2; OleDbCommand comm; OleDbConnection conn; protected void Page_Load(Object Src, EventArgs E) { if(Request。
      QueryString["action"]=="out"){ HttpCookie cookie =new HttpCookie("user"); cookie。Expires = DateTime。Now。AddDays(-1); Response。
      AppendCookie(cookie); Response。Redirect("index。aspx"); } if(Request。Cookies["user"]!=null){ value1=Convert。ToString(Request。
      Cookies["user"]["username"]); } conn=new OleDbConnection("Provider=Microsoft。Jet。OLEDB。4。0;Data Source="+Server。MapPath("data/db。
      mdb")); comm=new OleDbCommand("SELECT TOP 7 * from [news] where mytype='热点新闻' order by id DESC",conn); conn。Open(); OleDbDataReader grid1=comm。
      ExecuteReader(); DataGrid1。DataSource=grid1; DataGrid1。DataBind(); grid1。Close(); comm。CommandText="select top 7 * from [news] where mytype='新闻' order by id DESC"; OleDbDataReader grid2=comm。
      ExecuteReader(); DataGrid2。DataSource=grid2; DataGrid2。DataBind(); grid2。Close();。

    y***

    2010-09-27 11:04:00

  • 2010-09-26 20:28:00
  • 在visual stdio里可以用代码.也可以用数据库控件,用控件很简单不用写代码,可连SQL Access,最好用控件哦,我以前用不会时用的代码,现在都改用控件了

    王***

    2010-09-26 20:28:00

类似问题

换一换
  • 程序设计 相关知识

  • 电脑网络技术
  • 电脑网络

相关推荐

正在加载...
最新问答 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):