簡體   English   中英

C#到sqlite輸入字符串格式不正確

[英]c# to sqlite input string not in correct format

我正在嘗試將c#列表值傳輸到SQLite表。 (這里的菜鳥)

問題:

我收到以下錯誤:“輸入字符串的格式不正確。” 我試圖查看以前的帖子的解決方案,該解決方案與此錯誤相同,但到目前為止運氣不高。 我認為我的錯誤是列表最初具有字符串值,但后來我嘗試將整數sqlite列值輸入到其中。 我想將其輸入為字符串,有人可以用該語法幫助我-我只是不知道它。

這是清單:

         string[] fileLines = File.ReadAllLines(ofd.FileName);

            string[] section1 = fileLines.SkipWhile(line => !line.Contains("Seq#")).TakeWhile(line => !line.Contains("Total Records")).Where(line => Regex.IsMatch(line, @"[\s|\d]{4}\d")).Select(line => line.PadRight(198)).ToArray();   


            int[] pos = new int[10] { 0, 6, 18, 47, 53,57, 61, 68, 82, 97 }; //setlen&pos to read specific colmn vals
            int[] len = new int[10] { 6, 12, 29, 6 , 4, 4, 7, 14, 15, 6 };


            foreach (string line in section1)
            {
                //if (line.StartsWith("0")) break; // reads whole file, could slow it down ..

                for (int j = 0; j < 10; j++) 
                {
                    val[j] = line.Substring(pos[j], len[j]).Trim(); // add each column value in row to array
     //****    RIGHT HERE       list.Add(val[j]); // column values stored in list

                }

            }

這是SQLITE部分:

  private void button4_Click(object sender, EventArgs e)
    {
        // We use these three SQLite objects:
        SQLiteConnection sqlite_conn;
        SQLiteCommand sqlite_cmd;

        // create a new database connection: // Maybe error here - video was different
        sqlite_conn = new SQLiteConnection(@"Data Source=database.db;Version=3;");

        // 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 newtable1 (Seq integer primary key, Field integer , Description integer);";

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

        sqlite_cmd.CommandText = "INSERT INTO table1 (Seq, Field, Description) VALUES (@p1, @p2, @p3)";
        sqlite_cmd.Parameters.AddWithValue("@p1", 1);  // dummy initial values 
        sqlite_cmd.Parameters.AddWithValue("@p2", 2);
        sqlite_cmd.Parameters.AddWithValue("@p3", 3);
        for (int i = 0; i < 500; i += 3)
        {
            sqlite_cmd.Parameters["@p1"].Value = Convert.ToInt32(list[i]);
      // ***sqlite_cmd.Parameters["@p2"].Value = Convert.ToInt32(list[i + 1]); THE ERROR OCCURS HERE
            sqlite_cmd.Parameters["@p3"].Value = Convert.ToInt32(list[i + 2]); // 
            sqlite_cmd.ExecuteNonQuery();
        }
    }

關鍵要點:,我使用的是finisar.sqlite,但后來切換到SQLite的ADO.Net 2.0提供程序。

另外,如果我們可以讓Seq,Field和Key列包含字符串而不是整數,則它更可取。 我只是不知道將之后的所有內容都轉換為字符串的確切語法

要創建具有字符串的表,請執行以下操作:

string createTable = "CREATE TABLE newtable1 (Seq varchar(20) primary key, Field varchar(20), Description varchar(20))";

SQLiteCommand command = new SQLiteCommand(createTable, sqlite_conn);


command.ExecuteNonQuery();

這將使用字符串值創建它。

然后,您應該沒有問題:

         sqlite_cmd.CommandText = "INSERT INTO table1 (Seq, Field, Description) VALUES (@p1, @p2, @p3)";
                sqlite_cmd.Parameters.AddWithValue("@p1", "1");  // dummy initial values 
                sqlite_cmd.Parameters.AddWithValue("@p2", "2");
                sqlite_cmd.Parameters.AddWithValue("@p3", "3");
 for (int i = 0; i < 500; i += 3)
        {
            qlite_cmd.Parameters.AddWithValue("@p1", list[i]);
                sqlite_cmd.Parameters.AddWithValue("@p2", list[i+1]);
                sqlite_cmd.Parameters.AddWithValue("@p3", list[i+2]);
            sqlite_cmd.ExecuteNonQuery();
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM