连接access数据库及插入数据

it2022-05-05  141

//连接access数据库用的代码  1 OleDbConnection conn; // 声明一个OledbConnection对象  2             conn = new  OleDbConnection(); // 建立一个oledbConnection对象  3              // 设定连接字符串ConnectionString  4             conn.ConnectionString = " Provider=Microsoft.Jet.OLEDB.4.0; "  5      + " Data Source= "   +  Server.MapPath( " user.mdb " );  6             conn.Open();  7    OleDbCommand Cmd;  // 声明一个Command对象  8     // 建立Command对象,并且指定SQL语句  9     string  SQLStr; 10             SQLStr = " Select * From reg where userid=' "   +  username.Text  +   " ' " ; 11             Cmd = new  OleDbCommand(SQLStr,conn); 12             OleDbDataReader rd;  // 声明一个DataReader对象 13             rd = Cmd.ExecuteReader();   // 执行SQL指令,并将其结果设定给DataReader 14     if (rd.Read()) 15     Response.Write( " 该用户名已经存在,请重新输入! " ); 16     else 17     {18    if(password.Text!=repass.Text) 19     Response.Write("两次密码不相同,请重新输入!");20    else21    {22     step1.Visible=false;23     step2.Visible=true;24    }25   } //插入数据到access数据库 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( " 恭喜,您已经注册成功! " );

转载于:https://www.cnblogs.com/pageantry/archive/2007/05/16/748146.html

相关资源:vc基于access数据库添加数据操作

最新回复(0)