连接字符串属性尚未初始化

时间:2015-09-03 11:37:53

标签: c# sqlite

我不知道为什么这段代码会给我一个错误。 我试图将sql命令放入事务中。 这段代码给了我这个错误。 我不能在源头填写任何其他内容。 这是我得到的错误

  

附加信息:索引(从零开始)必须大于或等于零且小于参数列表的大小。

using (SQLiteConnection cn = new SQLiteConnection(string.Format("Data Source={0};")))
                    {
                        cn.Open();
                        using (SQLiteTransaction tr = cn.BeginTransaction())
                        {

                            sqlTarget = sqlInsert1 + columnList + ") VALUES (";
                            object[] sourceVal = new object[nCol];
                            rdrSourceTable.GetValues(sourceVal);
                            string strMsg = string.Empty;
                            int iCol = 0;
                            foreach (object col in sourceVal)
                            {
                                string columnName = rdrSourceTable.GetName(iCol++);
                                sqlTarget += objDbTarget.ObjectForSql(col, ref strMsg, false, columnName) +
                                             comma;
                            }
                            if (strMsg.Length > 0)
                            {
                                msg = string.Format(
                                    "The input values are wrong, strMsg = {0}\r\nThe composed sql = {1}",
                                    strMsg, sqlTarget);
                                if (m_interactive)
                                {
                                    DialogResult res = MessageBox.Show(msg, GetType().ToString(),
                                        MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                                    if (res == DialogResult.Cancel)
                                    {
                                        throw new CopyDbContentsException(msg);
                                    }
                                }
                                if (errorCount++ < 5)
                                {
                                    RadFile.WriteLogMsg("FillTableWithInsertCommands. " + msg +
                                                        "\r\n\r\nContinue?");
                                }
                                //Skip the insert action because of the error and go to next row.
                                continue;
                            }
                            sqlTarget = sqlTarget.Substring(0, sqlTarget.Length - comma.Length) + ")";
                            objDbTarget.ExecuteActionQuery(sqlTarget);
                            iRow++;
                            int remainder = iRow%250;
                            if (remainder == 0)
                            {
                                WriteStatusLabel(string.Format(
                                    "Copy the rows of table {0}. {1:N0} written.", Name,
                                    iRow));
                            }
                            remainder = iRow%nMsgMod;
                            if (remainder == 0)
                            {
                                msg = string.Format("{0:N0} rows of table {1} copied.",
                                    iRow, Name);
                                RadFile.WriteLogMsg(msg, withHeader: false);
                                if (nMsgMod < 100000 && iRow == 10*nMsgMod)
                                {
                                    nMsgMod *= 10;
                                }
                            }
                            tr.Commit();
                        }
                        cn.Close();
                    }
                }
                msg = string.Format("Table {0} is copied, {1:N0} rows. ", Name, iRow);
                        if (errorCount > 0)
                        {
                            msg += errorCount;
                            msg += (errorCount == 1) ? " row is" : " rows are";
                            msg += " skipped because of errors in the input";
                        }
                        RadFile.WriteLogMsg(msg, withHeader: false);
            }
        }

3 个答案:

答案 0 :(得分:2)

你希望这个有效吗?

SQLiteConnection cn = new SQLiteConnection(string.Format("Data Source={0};"))

这是关于SQLite ConnectionStrings

的一个很好的解释

https://www.connectionstrings.com/sqlite/

我假设您想要做类似

的事情
var cn = new SQLiteConnection(string.Format("Data Source={0};Version=3;", @"c:\mydb.db"))

错误

  

附加信息:索引(从零开始)必须大于或等于零且小于参数列表的大小。

string.Format("Data Source={0};")想要访问索引0处的第一个项目,而不是由您提供

答案 1 :(得分:1)

它在第一行:

string.Format("Data Source={0};"

您必须为字符串格式提供参数,例如:

string.Format("Data Source={0};","my data source")

其中&#34;我的数据源&#34;将是您的数据库数据源名称。

答案 2 :(得分:1)

伙计,你应该重构所有这些代码。但对于你的问题,这是解决方案:

using (SQLiteConnection cn = new SQLiteConnection("put your entire connection string here"))

有关如何使用string.Format方法的详细信息,请参阅this