gaoyanhui-999

统计

最新评论

SQL / ACCESS 数据库连接及增删改查


SQL / ACCESS 数据库连接
一、Sql数据库连接方法
(一)Sql中创建数据库:

create database login
use login
create table login
(
  userName varchar(20) primary key,
  userPwd  varchar(20) not null,
  userIdentity varchar(20)
)
insert into login values('wr','wr','教师')

(二)建立ASP.Net查找库中内容:

1、表单提交页代码

<form action="UserData.aspx" method="post">
  用户名<input id="Text1" name="username" type="text" />
  <br />
  密  码<input id="Password1" name="userpwd" type="password"/>
  <br />
  <input id="Submit1" type="submit" value="提交" />
</form>
2、数据库连接页面

    protected void Page_Load(object sender, EventArgs e)
    {   string username = Request.Form["username"];
        string userpwd = Request.Form.Get("userpwd");

        SqlConnection con = new  SqlConnection("server=.;database=login;uid=sa;pwd=;");
        try
        {  con.Open();
            Response.Write("数据库连接成功");    }
        catch
        {   Response.Write("数据库连接失败");   }

        SqlCommand com =
                new SqlCommand("select count(*) from login
                where username='" + username + "' and userpwd='" + userpwd + "'", con);

        int count = Convert.ToInt32(com.ExecuteScalar());
        if (count > 0)
        {   Response.Write("找到对应数据");    }
        else
        {   Response.Write("未找到对应数据"); }
    }
二、Access数据库连接方法

 (1)调用:
    using System.Data.OleDb;

 (2)数据库连接语句:
    protected void Page_Load(object sender, EventArgs e)
    {   string conn = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" + Server.MapPath("userdata.mdb");

        OleDbConnection myconn = new OleDbConnection(conn);

        try
        {  myconn.Open();
            Response.Write("现在数据库连接状态" + myconn.State);
        }
        catch (Exception ex)
        {  Response.Write("数据库连接状态" + ex.ToString());     }
        finally
        {  myconn.Close();    }
    }
三、使用SqlDataSource

    和 AccessDataSource

    控件连接数据库的方法


一.向空白数据库中插入记录 并转向显示页面
1   在SQL创建数据库
create database bookLibrary
use bookLibrary
create table bookLibrary
(
  bookID int primary key,
  bookTitle varchar(20) not null,
  bookSum int default 0 
)
2   建立信息输入页面updateBook.htm
建立编号、书名、数量文本框

建立提交按钮 和 重写按钮

在FORM表单中指定服务端接收文件,
及POST传送方式
3  建立数据库连接类文件 DB.cs
using System.Data.SqlClient;

public class DB
{
  public DB()
  {
  }
  public static SqlConnection createConnection()
  {
  SqlConnection con = new SqlConnection("server=WANG;database=bookLibrary;uid=sa;pwd=;");
  return con;
  }
}
4  在ASPX文件中使用 GridView 控 件 并添加button控件
定义为“添加”按钮
5    最后,在CS文件中制定任务
using System.Data.SqlClient;

public partial class bookShopping_upBook : System.Web.UI.Page
{    protected void Page_Load(object sender, EventArgs e)
     {  if (!IsPostBack)
        {
            string bookID = Request.Form["bookID"];
            string bookTitle = Request.Form["bookTitle"];
            string bookSum = Request.Form["bookSum"];
 string strComm = "Insert into bookLibrary(bookID,bookTitle,bookSum) values(" + bookID + ",'" + bookTitle + "'," + bookSum + ")";

            SqlConnection mycon = DB.createConnection();
            mycon.Open();

            SqlCommand oComm = new SqlCommand(strComm, mycon);
            oComm.ExecuteNonQuery();

            SqlCommand mycom = new SqlCommand("select * from bookLibrary", mycon);
            SqlDataReader Rs = mycom.ExecuteReader();
            this.GridView1.DataSource = Rs;
            this.GridView1.DataBind();
           mycon.Close();
        }
    }    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("updateBook.htm");
    }
}
服务器端接收程序 updata.aspx
二.修改&删除
一.插入记录

插入一条记录到数据库的SQL语句格式:
Insert Into 表名(字段1,字段2,...) values(值1,值2,...)

SQLCommand 可以执行SQL命令,把插入记录的SQL语句传递到SQLCommand的CommandText属性,然后执行其ExecuteNonQuery方法,就可以实现记录的插入.

例题:
string strConn;
string strComm;
strConn="server=.;database=login;uid=sa;pwd=;"
strComm="Insert Into login(username,userpwd,userjob) values('username','userpwd','userjob')";
SqlConnection oConn=new SqlConnection(strConn);
SqlCommand oComm=new SqlCommand(strComm,oConn);
oConn.Open();
oComm.ExecuteNonQuery();
oConn.Close();
二.修改记录

对数据库中的数据进行修改的SQL语句格式:
Update 表名 set 字段1=值1,字段2=值2,...where 条件

例题:
String strConn;
String strComm;
strConn="server=.;database=login;uid=sa;pwd=;";
strComm="update login set userpwd='abc',userjob='abc' where username='a'";
sqlConnection oConn=new SqlConnection(strConn);
sqlCommand oComm=new SqlCommand(strComm,oConn);
oConn.Open();
oComm.ExecuteNonQuery();
oConn.Close();
三.删除记录

从数据库中删除表中一部分记录的格式:
delete from 表名 where 条件

从数据库中删除表中所有记录的格式:
delete * from 表名

例题:
String strConn;
String strComm1;
String strComm2;
strConn="server=.;database=login;uid=sa;pwd=;";
strComm1="delete from login where username='a'";
strComm2="delete * from login";
SqlConnection oConn=new SqlConnection(strConn);
SqlCommand oComm1=new SqlCommand(strComm1,oConn);
SqlCommand oComm2=new SqlCommand(strComm2,oConn);
oConn.Open();
oComm1.ExecuteNonQuery();//删除一条记录
oComm2.ExecuteNonQuery();//删除表中的所有记录
oConn.Close();

 

 

posted on 2007-09-17 08:19 高彦辉 阅读(1889) 评论(1)  编辑 收藏 引用

评论

# re: SQL / ACCESS 数据库连接及增删改查 2009-05-26 15:36 dsa

dsa  回复  更多评论   

只有注册用户登录后才能发表评论。