把access或SQL SEVER数据库内容导出为XML的方法

 

同时把六个表的内容导出成XML文档,带着结构,以便于以后导入。这是在access下调试成功的。如果是sql sever,只要把select字符串修改一下就好。也包括了连接ACCESS数据库的方法。

你测试时要自行建立一下表,然后把表名与字段名改了就好,转载请注明蚂蚁资讯网原创,如果有兴趣的话,点一下你感兴趣的广告连接。

private void accesstoxml()//把ACCESS数据库的内容转成XML文档
        {
            //定义一下生成的文件名

            string xmlname = "myxml";
            //连接数据库
            OleDbConnection conn = null;
            bool connet = false;//表示数据库没连接上
            if (!connet)//如果没连上就连接
            {

                string loadSource = @"Jet OLEDB:Registry Path=;Data Source=" + path.currentpath + "\\mymdb.mdb" + ";Jet OLEDB:Database Password=1234567;Jet OLEDB:System database=;Jet OLEDB:Global Bulk Transactions=1;User ID=Admin;Provider=" + "Microsoft.Jet.OLEDB.4.0" + ";Jet OLEDB:Don’t Copy Locale on Compact=False;Jet OLEDB:SFP=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Engine Type=5;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:Global Partial Bulk Ops=2;Extended Properties=;Mode=Share Deny None;Jet OLEDB:Create System Database=False;Jet OLEDB:Database Locking Mode=1";

                //    loadform.progressBar1.Value=40;

                conn = new OleDbConnection(loadSource);
                try
                {
                    conn.Open();
                    connet = true;//连上了
                    //MessageBox.Show("connet");
                }
                catch (Exception e2)
                { MessageBox.Show(e2.Message.ToString()); }
                connet = true;

            }
            //else//如果已经连上了就开始读写

            if (conn != null)
            {

                try
                {                
                            
                    DataSet ds = new DataSet();

            
                    OleDbCommand comm = null;
                    comm = new OleDbCommand(@"SELECT * FROM [Crew_training_recorder] where [toxml]=’a’", conn);
                    OleDbDataAdapter da = new OleDbDataAdapter();
                    da.SelectCommand = comm;                    
                    da.Fill(ds, "Crew_training_recorder");

                    

                    comm = new OleDbCommand(@"SELECT * FROM [DeckLog] where [toxml]=’a’", conn);
                    da.SelectCommand = comm;
                    
                    da.Fill(ds, "DeckLog");

                

                    comm = new OleDbCommand(@"SELECT * FROM [EngineLog] where [toxml]=’a’", conn);
                    da.SelectCommand = comm;
                    da.Fill(ds, "EngineLog");
            

                    comm = new OleDbCommand(@"SELECT * FROM [Ship_Maintenance_Recorder] where [toxml]=’a’", conn);
                    da.SelectCommand = comm;
                    da.Fill(ds, "Ship_Maintenance_Recorder");

                


                    comm = new OleDbCommand("SELECT * FROM [Ship_Particullar]", conn);
                    da.SelectCommand = comm;
                    da.Fill(ds, "Ship_Particullar");

                

                    comm = new OleDbCommand(@"SELECT * FROM [Trouble_shooting_Recorder] where [toxml]=’a’", conn);
                    da.SelectCommand = comm;
                    da.Fill(ds, "Trouble_shooting_Recorder");


                    
                

                    read.toxml("Crew_training_recorder");
                    read.toxml("DeckLog");
                    read.toxml("EngineLog");
                    read.toxml("Ship_Maintenance_Recorder");
                    read.toxml("Ship_Particullar");
                    read.toxml("Trouble_shooting_Recorder");
                

                    if(File.Exists(path.currentpath.ToString().Trim() + @"\data\"+xmlname.Trim()+".xml"))
                    {
                        File.Delete(path.currentpath.ToString().Trim() + @"\data\"+xmlname.Trim()+".xml");
                    }
                    ds.WriteXml(path.currentpath.ToString().Trim() + @"\data\"+xmlname.Trim()+".xml", XmlWriteMode.WriteSchema);

                    comm.Dispose();
                    da.Dispose();
                    ds.Dispose();

                }
                catch (Exception e3)
                { MessageBox.Show(e3.Message.ToString()) ; }
                conn.Close();
                this.timer2.Enabled = false;

            }
            else
            {
                MessageBox.Show("The database is not connected,try again please.");
                this.timer2.Enabled = false;
            }
        } 
    

转载于:https://www.cnblogs.com/rzwince/articles/1647858.html

可以通过以下步骤使用 C# 将 SQL Server 数据库数据导出为 Excel 文件: 1. 使用 SQL Server Management Studio (SSMS) 创建一个 SQL 查询,该查询从特定表中选择要导出的数据。 2. 在 Visual Studio 中创建一个新的 C# 控制台应用程序项目。 3. 在该项目中添加对 Microsoft.Office.Interop.Excel 引用的引用。 4. 在代码中,创建一个 SqlConnection 对象并打开连接。 5. 创建一个 SqlCommand 对象,并将查询字符串和 SqlConnection 对象传递给它。 6. 使用 SqlCommand 对象的 ExecuteReader 方法执行查询,并将结果存储在 SqlDataReader 对象中。 7. 创建一个 Excel.Application 对象,并使用它创建一个新的工作簿。 8. 在工作簿中创建一个新的工作表,并将 SqlDataReader 对象中的数据写入该工作表中。 9. 保存 Excel 文件。 以下是 C# 代码示例: ``` using System; using System.Data.SqlClient; using Microsoft.Office.Interop.Excel; namespace SQLtoExcel { class Program { static void Main(string[] args) { string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;"; string queryString = "SELECT * FROM myTable"; SqlConnection connection = new SqlConnection(connectionString); connection.Open(); SqlCommand command = new SqlCommand(queryString, connection); SqlDataReader reader = command.ExecuteReader(); Application excel = new Application(); Workbook workbook = excel.Workbooks.Add(); Worksheet worksheet = (Worksheet)workbook.Worksheets[1]; int row = 1; int column = 1; for (int i = 0; i < reader.FieldCount; i++) { worksheet.Cells[row, column++] = reader.GetName(i); } row++; while (reader.Read()) { column = 1; for (int i = 0; i < reader.FieldCount; i++) { worksheet.Cells[row, column++] = reader[i].ToString(); } row++; } workbook.SaveAs("output.xlsx"); workbook.Close(); excel.Quit(); Console.WriteLine("Data exported to Excel file."); Console.ReadLine(); } } } ``` 请注意,这只是一个简单的示例,您可能需要根据您的需要进行更改和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值