[英]c# to sqlite input string not in correct format
I'm trying to transfer c# List values to an SQLite table. 我正在尝试将c#列表值传输到SQLite表。 (Noob here) (这里的菜鸟)
Problem: 问题:
I'm getting the following error: "Input string was not in a correct format." 我收到以下错误:“输入字符串的格式不正确。” I tried to look at the solutions to previous posts which had the same error as this but not much luck so far. 我试图查看以前的帖子的解决方案,该解决方案与此错误相同,但到目前为止运气不高。 I think my error is that the list initially has string values but later I try inputting into it integer sqlite columns values. 我认为我的错误是列表最初具有字符串值,但后来我尝试将整数sqlite列值输入到其中。 I would love to input it as a string, can someone help me with that syntax - i just don't kw it. 我想将其输入为字符串,有人可以用该语法帮助我-我只是不知道它。
Here is the list: 这是清单:
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
}
}
Here is the SQLITE part: 这是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();
}
}
Key notables:, I was using finisar.sqlite but then switched to ADO.Net 2.0 provider for SQLite. 关键要点:,我使用的是finisar.sqlite,但后来切换到SQLite的ADO.Net 2.0提供程序。
Also, it would prefer if we could get the Seq, Field, and Key columns to contain strings instead of integers. 另外,如果我们可以让Seq,Field和Key列包含字符串而不是整数,则它更可取。 I just don't know the exact syntax to transfer everything after that to string 我只是不知道将之后的所有内容都转换为字符串的确切语法
To create the Table to have strings, do this: 要创建具有字符串的表,请执行以下操作:
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();
This will create it with string values. 这将使用字符串值创建它。
Then you should have no problem doing this: 然后,您应该没有问题:
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.