数据库链接 小知识

C#:

如果转贴请注明出处

作者:fx_blog(CSDN)

日期:2005-4-27

//=========================查询===========================

<% @ Page Language="C#" debug=true %>

<% @ import namespace="System.Data"%>

<% @ import namespace="System.Data.OleDb"%>

<% @ import namespace="System.IO"%>

<script runat=server>

          void btn_search(Object sender,EventArgs e)

         {

              string connstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("**.mdb")+";";

              string selstring="select * from dbtable where username='"+TextBox1.Text+"'";

              OleDbConnection conn=new OleDbConnection(conn);

              OleDbCommand cmd=new OleDbCommand();

              cmd.CommandText = selstring;

             cmd.Connection = conn;

             try

             {

                    conn.Open();

                     OleDbDataReader reader=cmd.ExecuteReader();

                  if(reader.Read())

                 {

                      Label1.Text="查找成功";

                   }

                 else

                      {

                             Label1.Text="查询失败";

                    }

              }

              catch (OleDbException ex)

               {

                    Response.Write(ex.Errors[0].Message);

              }

         }

    </script>

<form runat=server>

<asp:TextBox id="TextBox1" runat=server />

<asp:Button id="btn_search" runat=server text="查询" onclick=btn_search />

</form>

//==========================添加,删除,=====================

<% @ Page Language="C#" debug=true %>

<% @ import namespace="System.Data"%>

<% @ import namespace="System.Data.OleDb"%>

<% @ import namespace="System.IO"%>

<script runat=server>

          void add_search(Object sender,EventArgs e)

         {

              string connstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("**.mdb")+";";

              string selstring="select * from dbtable where username='"+TextBox1.Text+"'";

             OleDbConnection conn=new OleDbConnection(conn);

              OleDbCommand cmd=new OleDbCommand();

              cmd.CommandText = selstring;

             cmd.Connection = conn;

              try

                 {

                    conn.Open();

                       OleDbDataReader reader=cmd.ExecuteReader();

                        if (reader.Read())

                          {

                                Label1.Text="数据库中已经有该记录了,请用其他用户名!";

                         }

                         else

                         {

                                reader.Close();

                                string addstring="insert into [atable](username)"+"values('"+TextBox1.Text+"')";

                               cmd=new OleDbCommand();

                              cmd.CommandText=addstring;

                             cmd.Connection=conn;

                              cmd.ExecuteNonQuery();

                               Label1.Text="添加成功";

                         }

                         conn.Close();

                  }

                catch(OleDbException ex)

              {

                         Response.Write(ex.Errors[0].Message);

               }

</script>

<form runat=server>

<asp:TextBox id="TextBox1" runat=server />

<asp:Button id="btn" text="添加" onclick="add_search" runat=server />

</form>

//===========================删除=更新===================

    

<% @ Page Language="C#" debug=true %>

<% @ import namespace="System.Data"%>

<% @ import namespace="System.Data.OleDb"%>

<% @ import namespace="System.IO"%>

<script runat=server>

          void add_search(Object sender,EventArgs e)

         {

              string connstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("**.mdb")+";";

              string selstring="select * from dbtable where username='"+TextBox1.Text+"'";

             OleDbConnection conn=new OleDbConnection(conn);

              OleDbCommand cmd=new OleDbCommand();

              cmd.CommandText = selstring;

             cmd.Connection = conn;

              try

                 {

                    conn.Open();

                       OleDbDataReader reader=cmd.ExecuteReader();

                        if (!reader.Read())

                          {

                                Label1.Text="数据库中没有该用户!";

                         }

                         else

                         {

                                reader.Close();

                                string addstring="delete * from atable where username='"+TextBox1.Text+"'";

                       // 如果是更新数据update * from atable where username='"+TextBox1.Text+"';

                               cmd=new OleDbCommand();

                              cmd.CommandText=addstring;

                             cmd.Connection=conn;

                              cmd.ExecuteNonQuery();

                               Label1.Text="操作成功";

                         }

                         conn.Close();

                  }

                catch(OleDbException ex)

              {

                         Response.Write(ex.Errors[0].Message);

               }

</script>

<form runat=server>

<asp:TextBox id="TextBox1" runat=server />

<asp:Button id="btn" text="执行" onclick="add_search" runat=server />

</form>

阅读更多
个人分类: 欢迎
想对作者说点什么? 我来说一句

数据库链接

2015年11月21日 699KB 下载

没有更多推荐了,返回首页

不良信息举报

数据库链接 小知识

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭