1

我有一张 Excel 表,我使用 OleDbConnection 从中获取数据集中的数据

private static DataSet ImportExcel(string FileName, bool hasHeaders)
        {
            string HDR = hasHeaders ? "Yes" : "No";
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName +
                             ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=1\"";

            DataSet output = new DataSet();

            using (OleDbConnection conn = new OleDbConnection(strConn))
            {
                conn.Open();

                DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });

                foreach (DataRow row in dt.Rows)
                {
                    string sheet = row["TABLE_NAME"].ToString();

                    OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + sheet + "]", conn);
                    cmd.CommandType = CommandType.Text;

                    DataTable outputTable = new DataTable(sheet);
                    output.Tables.Add(outputTable);

                    new OleDbDataAdapter(cmd).Fill(outputTable);
                }
            }
            return output;
        }

现在我想将此数据集中的数据插入到 sqlite 表中。

4

1 回答 1

0

这就是我们可以做到的

    SQLiteConnection m_dbConnection; 
    void createDbAndTable()
    {
        SQLiteConnection.CreateFile("MyDatabase.sqlite");
        m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite; Version=3;");
        m_dbConnection.Open();
        string sql = "create table myValues (name varchar(20), highScore int)";
        SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
        command.ExecuteNonQuery();
    }

    void fillTable(DataSet ds)
    {
        var dt = ds.Tables[0];

        foreach (DataRow dr in dt.Rows)
        {
            var name = dr["name"].ToString();
            var score = Convert.ToInt32(dr["value"].ToString());
            string sql = "insert into myValues (name, highScore) values ( '" + name + "'," + score + ")";
            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();
        }
        m_dbConnection.Close();
    }
于 2013-03-28T11:49:05.867 回答