【问题标题】:How to create sqlite file in web application instead of windows application in c#如何在 web 应用程序中创建 sqlite 文件,而不是在 c# 中的 windows 应用程序
【发布时间】:2016-04-07 02:24:12
【问题描述】:
protected void callWebService()
{
    DateTime dt = System.DateTime.Now;
    WebReference.plFairPriceShopDetails[] arr = obj.FairPriceShopRequestAck("158500100002", "3058505610803243", "ECIL", "84:EB:18:EC:54:3A", "v1.0", dt, "alloc032016", dt, dt, 1);

    DataTable dataT = new DataTable();
    dataT = objToDataTable(arr);


    string connStr = @" DataSource = C:\Users\gobol\Desktop\UK.db; Version =3";
    //After finister

    Finisar.SQLite.SQLiteConnection sqlite_conn;
    Finisar.SQLite.SQLiteCommand sqlite_cmd;
    Finisar.SQLite.SQLiteDataReader sqlite_datareader;

    // create a new database connection:
    sqlite_conn = new Finisar.SQLite.SQLiteConnection("Data Source=database.db;Version=3;New=True;Compress=True;");
    //  sqlite_conn = new Finisar.SQLite.SQLiteConnection(connStr);

    // open the connection:
    sqlite_conn.Open();

    // create a new SQL command:
    sqlite_cmd = sqlite_conn.CreateCommand();

    // Let the SQLiteCommand object know our SQL-Query:
    sqlite_cmd.CommandText = "CREATE TABLE test (id integer primary key, text varchar(100));";

    // Now lets execute the SQL ;D
    sqlite_cmd.ExecuteNonQuery();

    // Lets insert something into our new table:
    sqlite_cmd.CommandText = "INSERT INTO test (id, text) VALUES (1, 'Test Text 1');";

    // And execute this again ;D
    sqlite_cmd.ExecuteNonQuery();

    // ...and inserting another line:
    sqlite_cmd.CommandText = "INSERT INTO test (id, text) VALUES (2, 'Test Text 2');";

    // And execute this again ;D
    sqlite_cmd.ExecuteNonQuery();

    // But how do we read something out of our table ?
    // First lets build a SQL-Query again:
    sqlite_cmd.CommandText = "SELECT * FROM test";

    // Now the SQLiteCommand object can give us a DataReader-Object:
    sqlite_datareader = sqlite_cmd.ExecuteReader();

    // The SQLiteDataReader allows us to run through the result lines:
    while (sqlite_datareader.Read()) // Read() returns true if there is still a result line to read
    {
        // Print out the content of the text field:
        //System.Console.WriteLine(sqlite_datareader["text"]);
        string op = sqlite_datareader.GetString(0);
    }

    // We are ready, now lets cleanup and close our connection:
    sqlite_conn.Close();
}

我必须在与soap api交互时在系统中创建一个sqlite文件。这里Soap api返回一些数据所以我必须从这些数据中创建数据库文件。我可以通过 windows 应用程序创建,但不能通过 web 应用程序创建,因为之后我必须在调用 web 应用程序的特定设备上上传数据库文件。

【问题讨论】:

    标签: c# asp.net web-services sqlite soap


    【解决方案1】:

    在 IIS Express 中 Program Files 文件夹下下载的文件

          public bool connectToSQLiteCreateDB(string DBName)
        {
            // create a new database connection:
            sqlite_conn = new SQLiteConnection("Data Source=" + DBName + ".db;Version=3;New=True;Compress=True;");
    
            try
            {
                // open the connection:
                sqlite_conn.Open();
    
                // create a new SQL command:
                sqlite_cmd = sqlite_conn.CreateCommand();
    
                // Let the SQLiteCommand object know our SQL-Query:
    
                // Create Entilement Table 
                sqlite_cmd.CommandText = "CREATE TABLE entitlement (" +
          "commodity_name_en    character varying," +
          "commodity_name_ll    character varying," +
          );";
    
                // Now lets execute the SQL ;D
                sqlite_cmd.ExecuteNonQuery();
                sqlite_conn.Close();
                return true;
    
            }catch(Exception ex)
            {
                 var err=ex.Message;
                 return false;
            }
                   }
    
         public void connectToSQLiteInsertIntoDB(string DBName)
        {
            sqlite_conn = new SQLiteConnection("Data Source=" + DBName + ".db;Version=3;New=False;");
    
            // open the connection:
            sqlite_conn.Open();
    
            // create a new SQL command:
            sqlite_cmd = sqlite_conn.CreateCommand();
    
            //// Lets insert something into our new table:
            //sqlite_cmd.CommandText = "INSERT INTO test (id, text) VALUES (1, 'Test Text 1');";
    
            //// And execute this again ;D
            //sqlite_cmd.ExecuteNonQuery();
    
            //// ...and inserting another line:
            //sqlite_cmd.CommandText = "INSERT INTO test (id, text) VALUES (2, 'Test Text 2');";
    
            //// And execute this again ;D
            //sqlite_cmd.ExecuteNonQuery();
    
            //// But how do we read something out of our table ?
            //// First lets build a SQL-Query again:
            //sqlite_cmd.CommandText = "SELECT * FROM test";
    
            //// Now the SQLiteCommand object can give us a DataReader-Object:
            //sqlite_datareader = sqlite_cmd.ExecuteReader();
    
            //// The SQLiteDataReader allows us to run through the result lines:
            //while (sqlite_datareader.Read()) // Read() returns true if there is still a result line to read
            //{
            //    // Print out the content of the text field:
            //    //System.Console.WriteLine(sqlite_datareader["text"]);
            //    string op = sqlite_datareader.GetString(0);
            //}
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-06
      • 2011-01-29
      • 1970-01-01
      • 2011-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多