c# 数据库的写入到excel
access数据库结构表名ZZZ 结构内容:ID FF DD1 2 34 5 67 8 9新建一个c#窗体应用程序添加一个ComboBox控件 和三个button按键 还有三个textbox文本框button1 --->access数据库位置button2 --->EXCEL位置button3 --->导入textbox1--->access数据库的路径textbox2--->EXCEL的路径textbox3--->EXCEL文件名ComboBox1--->显示数据库表名自定义函数1:[csharp]public void AccessGuideJoinExcel(string Access, string AccTable, string Excel){try{string tem_sql = "";//定义字符串string connstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Access + ";Persist Security Info=True";//记录连接Access的语句System.Data.OleDb.OleDbConnection tem_conn = new System.Data.OleDb.OleDbConnection(connstr);//连接Access数据库System.Data.OleDb.OleDbCommand tem_comm;//定义OleDbCommand类tem_conn.Open();//打开连接的Access数据库tem_sql = "select Count(*) From " + AccTable;//设置SQL语句,获取记录个数tem_comm = new System.Data.OleDb.OleDbCommand(tem_sql, tem_conn);//实例化OleDbCommand类int RecordCount = (int)tem_comm.ExecuteScalar();//执行SQL语句,并返回结果//每个Sheet只能最多保存65536条记录。tem_sql = @"select top 65535 * into [Excel 8.0;database=" + Excel + @".xls].[Sheet2] from ZZZ";//记录连接Excel的语句tem_comm = new System.Data.OleDb.OleDbCommand(tem_sql, tem_conn);//实例化OleDbCommand类tem_comm.ExecuteNonQuery();//执行SQL语句,将数据表的内容导入到Excel中tem_conn.Close();//关闭连接tem_conn.Dispose();//释放资源tem_conn = null;MessageBox.Show("导入完成");}catch (Exception ex){MessageBox.Show(ex.Message, "提示!");}}public void AccessGuideJoinExcel(string Access, string AccTable, string Excel){try{string tem_sql = "";//定义字符串string connstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Access + ";Persist Security Info=True";//记录连接Access的语句System.Data.OleDb.OleDbConnection tem_conn = new System.Data.OleDb.OleDbConnection(connstr);//连接Access数据库System.Data.OleDb.OleDbCommand tem_comm;//定义OleDbCommand类tem_conn.Open();//打开连接的Access数据库tem_sql = "select Count(*) From " + AccTable;//设置SQL语句,获取记录个数tem_comm = new System.Data.OleDb.OleDbCommand(tem_sql, tem_conn);//实例化OleDbCommand类int RecordCount = (int)tem_comm.ExecuteScalar();//执行SQL语句,并返回结果//每个Sheet只能最多保存65536条记录。tem_sql = @"select top 65535 * into [Excel 8.0;database=" + Excel + @".xls].[Sheet2] from ZZZ";//记录连接Excel的语句tem_comm = new System.Data.OleDb.OleDbCommand(tem_sql, tem_conn);//实例化OleDbCommand类tem_comm.ExecuteNonQuery();//执行SQL语句,将数据表的内容导入到Excel中tem_conn.Close();//关闭连接tem_conn.Dispose();//释放资源tem_conn = null;MessageBox.Show("导入完成");}catch (Exception ex){MessageBox.Show(ex.Message, "提示!");}}自定义函数2:[csharp]public void GetTable(string Apath, ComboBox ComBox){string connstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Apath + ";Persist Security Info=True";System.Data.OleDb.OleDbConnection tem_OleConn = new System.Data.OleDb.OleDbConnection(connstr);tem_OleConn.Open();DataTable tem_DataTable = tem_OleConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });tem_OleConn.Close();ComBox.Items.Clear();for (int i = 0; i < tem_DataTable.Rows.Count; i++){ComBox.Items.Add(tem_DataTable.Rows[i][2]);补充:软件开发 , C# ,
上一个:[C#]创建并调用dll文件
下一个:简单工厂、工厂方法与抽象工厂模式对比