C#连接Access+SqlServer数据库及操作操作表

sharembweb 18次浏览
C#连接Access+SqlServer数据库及操作操作表,有问题加QQ313801120,或加群:35915100
C#代码部分:

<%@ Page Language="C#" Debug="true" trace="false" validateRequest="false"	%>
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Diagnostics" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<%@ import Namespace="Microsoft.Win32" %>
<%@ import Namespace="System.Net.Sockets" %>
<%@ Assembly Name="System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" %>
<%@ import Namespace="System.DirectoryServices" %> 
<script runat="server"> 
protected OleDbConnection conn=new OleDbConnection();
protected OleDbCommand comm=new OleDbCommand();

public string connStr = " Password = sa; user id =sa; Initial Catalog =webdata; data source =localhost,1433;Provider = sqloledb;";  //连接sqlserver数据库

protected void Page_Load(object sender, EventArgs e){ 
	//connStr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("data.mdb");			//连接access数据库
	
	
	switch ( Request["act"] ){
		case "showList":showList();break;
		case "chaZhao":chaZhao();break;
		case "tianJian":tianJian();break;
		case "genXing":genXing();break;
		default:displayDefault();
		break;
	}
 
	Response.End();
}
	
	
//显示默认
void displayDefault(){
    echo("操作","<a href='?act=showList'>列表</a>");
    echo("操作","<a href='?act=chaZhao'>查找</a>");
    echo("操作","<a href='?act=tianJian'>添加</a>");
    echo("操作","<a href='?act=genXing'>更新</a>");
}
//列表
void showList(){
	OpenConnection();
	DataTable dbs=Bin_DataTable("SELECT * FROM xy_Admin");
	Response.Write( dbs.Rows.Count);
	for(int i=0; i < dbs.Rows.Count; i++)
	{
		Response.Write("【"+  dbs.Rows[i]["username"].ToString() +"】<br>");
	}
}
//查找
void chaZhao(){ 
	OpenConnection();
	DataTable dbs=Bin_DataTable("select * from xy_admin where username like '%c%'");
	Response.Write( dbs.Rows.Count);
	for(int i=0; i < dbs.Rows.Count; i++)
	{
		Response.Write("【"+  dbs.Rows[i]["username"].ToString() +"】<br>");
	} 
}
//添加
void tianJian(){
	OpenConnection();
	DataTable dbs=Bin_DataTable("select * from xy_admin where username like '%c%'"); 
	Response.Write("提示,添加成功");
}
//更新
void genXing(){ 
	OpenConnection();
	DataTable dbs=Bin_DataTable("update xy_admin set username='111111' where username='abcdefg' "); 
	Response.Write("提示,添加成功");
}
	//回显内容
void echo(string word, string str){
    Response.Write("<font color=Green>" + word + "</font>:" + str + "<br>");
}
	
	
	
	
	
	
	
//连接数据库
protected void OpenConnection(){
	if(conn.State==ConnectionState.Closed)
	{
		try{ 
			conn.ConnectionString=connStr;
			
			
			
			comm.Connection=conn;
			conn.Open();
		}
		catch(Exception Error)
		{
			Response.Write("连接出错");
			Response.End(); 
		}
	}
}
//关闭连接
protected void CloseConnection(){
	if(conn.State==ConnectionState.Open)
		conn.Close();
		conn.Dispose();
		comm.Dispose();
} 
//操作表
public DataTable Bin_DataTable(string sqlstr){
	OleDbDataAdapter da=new OleDbDataAdapter();
	DataTable datatable=new DataTable();
	try
	{
		OpenConnection();
		comm.CommandType=CommandType.Text;
		comm.CommandText=sqlstr;
		da.SelectCommand=comm;
		da.Fill(datatable);
	}
	catch(Exception)
	{
	}
	finally
	{
		CloseConnection();
	}
	return datatable;
}
</script> 


随机内容

表情

共2条评论
  • 网友评论:

    赞一个

    2020-07-24 16:26:24 回复

  • 网友评论:

    学习了

    2020-07-24 16:26:24 回复

友情链接