连接access数据库及插入数据 .txt

连接access数据库用的代码


[运行代码] [复制到剪贴板] [ ± ]CODE:
OleDbConnection conn;//声明一个OledbConnection对象
            conn=new OleDbConnection();//建立一个oledbConnection对象
            //设定连接字符串ConnectionString
            conn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;"
    +"Data Source=" + Server.MapPath("user.mdb");
            conn.Open();
    OleDbCommand Cmd; //声明一个Command对象
    //建立Command对象,并且指定SQL语句
    string SQLStr;
            SQLStr="Select * From reg where userid='" + username.Text + "'";
            Cmd=new OleDbCommand(SQLStr,conn);
            OleDbDataReader rd; //声明一个DataReader对象
            rd=Cmd.ExecuteReader();  //执行SQL指令,并将其结果设定给DataReader
    if(rd.Read())
    Response.Write("该用户名已经存在,请重新输入!");
    else
    {
    if(password.Text!=repass.Text)
      Response.Write("两次密码不相同,请重新输入!");
    else
    {
      step1.Visible=false;
      step2.Visible=true;
    }
    }


插入数据到access数据库


[运行代码] [复制到剪贴板] [ ± ]CODE:
OleDbConnection conn;//声明一个OledbConnection对象
conn=new OleDbConnection();//建立一个oledbConnection对象
//设定连接字符串ConnectionString
conn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;"
+"Data Source=" + Server.MapPath("user.mdb");
conn.Open();
OleDbCommand Cmd; //声明一个Command对象
//建立Command对象,并且指定SQL语句
string SQLStr;
SQLStr="Insert into reg values('" + username.Text + "','";
SQLStr += password.Text + "','" + Email.Text + "','";
SQLStr += addr.Text + "','" +phone.Text + "','";
SQLStr += sex.SelectedItem.Text +"','" + Birth.Text + "')";

Cmd=new OleDbCommand(SQLStr,conn);
Cmd.ExecuteNonQuery();  //执行SQL指令
Response.Write("恭喜,您已经注册成功!");

posted on 2008-07-28 14:50  nathally  阅读(177)  评论(0)    收藏  举报

导航