C# webService 读取txt/Excel/SQL/Orcal的方法

1.  C#读取txt

2.  C#读取Excel

string strOdbcCon = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\Others\\xml data.xlsx; Extended Properties=Excel 8.0"; //定义OleDbConnection对象实例并连接Excel表格

OleDbConnection OleDB = new OleDbConnection(strOdbcCon);

 //定义OleDbDataAdapter对象实例并调用Select查询语句提取Excel数据信息

OleDB.Open();

String strExcel = "select * from[sheet1$] where UserNo="+username;

XmlDocument myXmlDocument = new XmlDocument();

DataSet ds = new DataSet();

OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, strOdbcCon);

OleDB.Close();

myCommand.Fill(ds);           

myXmlDocument.LoadXml(ds.GetXml());

return myXmlDocument;

3. C# 读取 SQL

          string sConnect = string.Format("server=***;database=*****");

                string sqlcommand = "select * from [iexec].[dbo].[SAP_Transfer_Log]";

                XmlDocument myXmlDocument = new XmlDocument();

                SqlConnection conn = new SqlConnection(sConnect);

                DataSet DT = new DataSet();

                if ("open" != conn.State.ToString().ToLower())

                {

                    conn.Open();

                }

                SqlCommand MyCommand = new SqlCommand(sqlcommand, conn);

                SqlDataAdapter MyDataAdapter = new SqlDataAdapter(MyCommand);

                MyDataAdapter.Fill(DT);

                conn.Close();

                myXmlDocument.LoadXml(DT.GetXml());

                return myXmlDocument;

4.C#读取Orcal

             DataSet dsNorthwind = new DataSet();

            //Create the connection string.          

             String sConnect;

            sConnect = "Password=eaiuser;User ID=eaiuser;Data Source=eaitest";

            //Create a connection object to connect to the northwind db.

            OracleConnection nwconnect = new OracleConnection(sConnect);

            //Create a command string to select all the customers in the WA region.

            String sCommand = "Select * from users";

            //Create an adapter to load the DataSet.

            OracleDataAdapter myDataAdapter = new OracleDataAdapter(sCommand, nwconnect);

            //Fill the DataSet with the selected records.

            myDataAdapter.Fill(dsNorthwind, "users");

            //Load the document with the DataSet.

            XmlDataDocument doc = new XmlDataDocument(dsNorthwind);

            //Display the XmlDataDocument.

            doc.Save(Console.Out);

posted @ 2014-04-08 13:47  ATS-IT-MES  阅读(578)  评论(0编辑  收藏  举报