2

スプレッドシートから大量のデータを取得しようとしていますが、C# コードで正常に接続できません。B

以下は、接続文字列と、接続を確立するために使用しているコードです。このプログラムの目的は、スプレッドシートからデータを取得し、それを SQL データベースに格納することです。connection.open() コマンドを通過できませんが、次のエラー メッセージが表示されません。

「外部テーブルが予期された形式ではありません」

        string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\nearod\Desktop\TestLoad.xlsx;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
        string queryString = "SELECT * FROM [SQL AgentUnique ID Test Load$]";
        try
        {
            OleDbDataReader reader;
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {

                OleDbCommand command = new OleDbCommand(queryString, connection);
                connection.Open();
                reader = command.ExecuteReader();


                while (reader.Read())
                {
                    counter++;

                    //Access db values
                    string compCode = "";
                    string agId = "";
                    string fName = "";
                    string lName = "";
                    string nameSuffix = "";


                    compCode = reader.GetValue(0).ToString();
                    agId = reader.GetString(1);
                    fName = reader.GetString(2);
                    lName = reader.GetString(3);
                    nameSuffix = reader.GetString(4);

                    sqlComm.Parameters.Add(companyCode);
                    sqlComm.Parameters.Add(agentID);
                    sqlComm.Parameters.Add(firstName);
                    sqlComm.Parameters.Add(lastName);
                    sqlComm.Parameters.Add(suffix);

                    //Initialize connection objects
                    cm = Dts.Connections["QUAHILSQ03"];
                    sqlConn = (SqlConnection)cm.AcquireConnection(Dts.Transaction);
                    sqlComm = new SqlCommand("AgentResourcesU01.dbo.sp_AgentIdAprCheck", sqlConn);
                    sqlComm.CommandType = CommandType.StoredProcedure;

                    //Execute stored procedure
                    sqlComm.ExecuteNonQuery();

                }
                reader.Close();
                connection.Close();
                OleDbConnection.ReleaseObjectPool();
            }
4

2 に答える 2

4

*.xlsx の場合、Ace ドライバーが必要です: Microsoft.ACE.OLEDB.12.0

string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;
 Data Source=C:\Users\nearod\Desktop\TestLoad.xlsx;
 Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
于 2012-09-26T14:51:46.987 に答える
0

これはかなり前に書いたものです。それは WebForms にありますが、.cs ファイルには、Excel スプレッドシートをデータセット、データテーブル、および多次元配列に変換する必要があることが示されています。

string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+FileToConvert+";Extended Properties=Excel 8.0;";
try
{
    OleDbConnection connection = new OleDbConnection(connectionString);
    connection.Open();
    //this next line assumes that the file is in default Excel format with Sheet1 as the first sheet name, adjust accordingly
    OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connection);
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    adapter.Fill(ds);//now you have your dataset ds now filled with the data and ready for manipulation
    // do stuff
}
于 2012-09-26T14:56:02.843 に答える