数据库的连接代码

string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +connection . load + "";//连接数据库,load是数据库的地址,此为无密码登陆

//"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath("~/App_Data/judgement_system.mdb") + ";User Id=admin;Password=;";//System.Web.HttpContext.Current.Server.MapPath("~/App_Data/judgement_system.mdb")为获取程序所在目录下的地址,相当于debug的地址。此为有密码登陆

又返回数据的连库操作:

DataSet de = new DataSet(); //建立数据集,这主要用于又返回值的连库
OleDbConnection myconnection = new OleDbConnection(ConnectionString);//建立连接对象,并将连接串给它
OleDbCommand mycommand = new OleDbCommand();//建立Command,主要执行SQL语句
https://www.360docs.net/doc/4e7861781.html,mandType = CommandType.Text;//SQL为文本型
https://www.360docs.net/doc/4e7861781.html,mandText = mysql; //将SQL语句给command,mysql为你的SQL语句
mycommand.Connection = myconnection;//把连接给command
OleDbDataAdapter myadapter = new OleDbDataAdapter(mysql, myconnection);//与下一句所起的作用相同,不过想要使用adapter的updata方法时,最好adapter是独立的
myadapter.SelectCommand = mycommand;
OleDbCommandBuilder builder = new OleDbCommandBuilder(myadapter);
myadapter.Fill(de, "score");//成绩表的连接

如果使用静态方法可如此设置:

public static string mysql;
public static DataSet myset=new DataSet ();//静态数据集
public static OleDbConnection myconnection = new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+System.Web.HttpContext.Current.Server.MapPath("~/App_Data/judgement_system.mdb") + "");//建立连接
public static OleDbCommand mycommand = new OleDbCommand();
public static OleDbDataAdapter myadapter = new OleDbDataAdapter();
public static void connection() //查询并且需填充dataset的连接方法
{
https://www.360docs.net/doc/4e7861781.html,mandType = CommandType.Text;
https://www.360docs.net/doc/4e7861781.html,mandText = mysql;
mycommand.Connection = myconnection;
myadapter.SelectCommand = mycommand;
OleDbCommandBuilder builder = new OleDbCommandBuilder(myadapter);

}
使用时:
mysql="select * from list";
connection();
myadapter.Fill(myset,"list");
myconnection.close(); //千万别忘记关闭连接

无返回数据的连库操作:

public static string mysql;
public static OleDbConnection myconnection = new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+System.Web.HttpContext.Current.Server.MapPath("~/App_Data/judgement_system.mdb") + "");//建立连接
public static OleDbCommand mycommand = new OleDbCommand();
public static int nonconnection()//利用返回值temp来判断是否成功
{
mycommand = new OleDbCommand(mysql, myconnection);
https://www.360docs.net/doc/4e7861781.html,mandText = mysql;
myconnection.Open();
int Temp = mycommand.ExecuteNonQuery();
myconnection.Close();

}

使用时:
mysql = "update Superuser set PW='000' where UserId='008'";
int temp = Connection.nonconnection();
if (temp == 1)
{
HttpContext.Current.Response.Write("");

}
else
{
HttpContext.Current.Response.Write("");
}

如果数据库使用的事SQL sever:

连接字符串:

ConnectionString = "Persist Security Info=False;Integrated Security=SSPI;database=trace;server=localhost;Connect Timeout=30";//此为windows登陆,连接的是trace库

connectionString="Data Source=server;Initial Catalog=DB07;Persist Security Info=True;User ID=sa;Password=sa"//此为数据库登陆,注意密码,用户使用为sa





















相关文档
最新文档