C# 到 sqlite 输入字符串的格式不正确

标签 c# sqlite

我正在尝试将 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 列包含字符串而不是整数,那就更好了。我只是不知道将其后的所有内容传输到字符串的确切语法

最佳答案

要创建包含字符串的表,请执行以下操作:

using System.Data.SQLite;

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();
}

关于C# 到 sqlite 输入字符串的格式不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19710013/

相关文章:

c# - Visual Studio 2008 中无法识别的标记前缀或设备筛选器

c# - 使用嵌入式资源打开 Excel 工作簿文件

spring - 如何为 SQLite 配置相关数据源文件?

sql - 如何将 'remember' 值插入到sqlite中的两个表中?

android - 在原始文件夹中打开数据库并对其执行不同的读取操作

Android 数据库版本控制

android - 为Android Sqlite数据库设置密码

c# - 在 Windows 上挂载/卸载 USB 磁盘

c# - 如何防止 Zalgo 文本等变音符号

c# - 将项目添加到用于 Parallel.ForEach c# 的 ConcurrentBag