2015-09-03 137 views
0

我不知道为什么此代码给我一个错误。 我想把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); 
      } 
     } 
+1

我认为您需要提供一个值,以连接字符串格式中的{0}替换。 –

+0

你不能'string.Format(“Data Source = {0};”)''你没有为'{0}'指定任何东西。 –

+0

这不是所有的代码。它包含超过10000行代码。 – astijn3457

回答

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")) 

和错误

添加一个很好的解释ITional information:索引(基于零)必须大于或等于零并小于参数列表的大小。

string.Format("Data Source={0};")想对在索引0,这是不是你

+0

我想通过将事务放入事务来增加sqlite的写入速度。我现在得到一个错误,说数据库锁定 – astijn3457

+1

有很多关于该问题的现有答案http://stackoverflow.com/questions/17592671/sqlite-database-locked-exception – fubo

1

其在1号线提供访问的第一个项目:

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

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

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

其中“我的数据源”将是您的数据库数据源名称。

1

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

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

看到this关于如何使用的String.format方法的详细信息。

相关问题