2012-03-22 125 views
0

的大小,如下所示,附连到“.ACCDB”文件大于或等于零且小于;当我运行它时,我收到消息:错误:其他信息:索引(基于零)必须大于鉴于<strong>C#</strong>代码参数列表

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

发生了什么事?

public partial class Form1 : Form 
    { 
    OleDbConnection vcon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;data source=C:\Hazardous Materials\KinneyDatabase.accdb"); 

     public Form1() 
      { 
       InitializeComponent(); 
      } 

     private void Form1_Load(object sender, EventArgs e) 
      { 
       { 
        vcon.Open(); 
       } 
       try 
       { 
        StreamReader sr = new StreamReader(@"C:\Hazardous Materials\cities.txt"); 
        string line = sr.ReadLine(); 

        StreamReader sr2 = new StreamReader(@"C:\Hazardous Materials\drugs.txt"); 
        string line2 = sr2.ReadLine(); 

        while (line != null) 
        { 
         comboBox1.Items.Add(line); 
         line = sr.ReadLine(); 
        } 
        while (line2 != null) 
        { 
         comboBox2.Items.Add(line2); 
         line2 = sr2.ReadLine(); 
        } 
        { 
         textBox2.Text = "Date"; 
        } 
       } 
      catch (System.Exception ex) 
       { 
        MessageBox.Show("Error: " + ex.Message); 
       } 
      } 

     private void button1_Click(object sender, EventArgs e) 
      { 
       string addRemove = ""; 

        if (radioButton1.Checked) 
         { 
          addRemove = radioButton1.Text; 
         } 
        else if (radioButton2.Checked) 
         { 
          addRemove = radioButton2.Text; 
         } 
       { 
        MessageBox.Show("You have entered the following information: \n\n" 
         + " Date: " + textBox2.Text + "\n" 
         + " Store#: " + comboBox1.Text + "\n" 
         + " Medication: " + comboBox2.Text + "\n" 
         + " Quantity: " + textBox1.Text + "\n" 
         + " Initials: " + textBox3.Text + "\n" 
         + " Initials: " + addRemove); 

       } 

      } 
     private void button2_Click(object sender, EventArgs e) 
      { 
       new Form2().Show(); 
      } 

     private void button3_Click(object sender, EventArgs e) 
      { 
      Application.Exit(); 
      } 

     private void toolStripMenuItem1_Click(object sender, EventArgs e) 
      { 
       MessageBox.Show("Scripted by Geoff Bertollini. March 2012"); 
      } 

     private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
      { 

      } 

     private void textBox2_TextChanged(object sender, EventArgs e) 
      { 
       var date = DateTime.Now.ToString("MM/dd/yyyy"); 
       textBox2.Text = date; 
      } 

     private void label4_Click_1(object sender, EventArgs e) 
     { 

     } 

     private void exitToolStripMenuItem1_Click(object sender, EventArgs e) 
     { 
      Application.Exit(); 
     } 

     private void button4_Click(object sender, EventArgs e) 
     { 
      { 
       string addRemove = ""; 

       if (radioButton1.Checked) 
       { 
        addRemove = radioButton1.Text; 
       } 
       else if (radioButton2.Checked) 
        { 
         addRemove = radioButton2.Text; 
        }     

       string vsql = string.Format("insert into Log values ({0}','{1}','{2}','{3}','{4}','{5}','{6}'),comboBox1.Text, comboBox2.Text, int.Parse(textBox1.Text), int.Parse(textBox1.Text), textBox2.Text, textBox3.Text, addRemove"); 
       OleDbCommand vcom = new OleDbCommand(vsql, vcon); 
       vcom.ExecuteNonQuery(); 
       MessageBox.Show("The following data has been saved to the database: \n\n" 
         + "Date: " + textBox2.Text + "\n" 
         + "Store#: " + comboBox1.Text + "\n" 
         + "Medication: " + comboBox2.Text + "\n" 
         + "Quantity: " + textBox1.Text + "\n" 
         + "Initials: " + textBox3.Text); 
       vcom.Dispose(); 
      } 
     } 
    } 
} 
+0

在你的代码的行不例外发生的? – Treb 2012-03-22 12:57:00

+0

还有一个可爱的sql注入漏洞。 – Will 2012-03-22 12:59:29

回答

1

既然你得到,System.FormatException,我想看看这行代码:

string vsql = string.Format("insert into Log values ({0}','{1}','{2}','{3}','{4}','{5}','{6}'),comboBox1.Text, comboBox2.Text, int.Parse(textBox1.Text), int.Parse(textBox1.Text), textBox2.Text, textBox3.Text, addRemove"); 

它可能应该是类似以下(格式化,使其更具可读性):

string vsql = string.Format("insert into Log values " + 
    "('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", 
    comboBox1.Text, 
    comboBox2.Text, 
    int.Parse(textBox1.Text), 
    int.Parse(textBox1.Text), 
    textBox2.Text, 
    textBox3.Text, 
    addRemove); 

你最初没有向format数组提供任何元素,因为它们是字符串本身的一部分(这可能不是你想要的)。

1

您的应用程序中有一个小格式错误和一个巨大的逻辑错误

巨大的逻辑错误是,您将来自用户输入的值直接传递给SQL查询字符串;这意味着你很容易受到SQL注入的影响。

这个小格式错误是在这里:

string vsql = string.Format("insert into Log values ({0}','{1}','{2}','{3}','{4}','{5}','{6}'),comboBox1.Text, comboBox2.Text, int.Parse(textBox1.Text), int.Parse(textBox1.Text), textBox2.Text, textBox3.Text, addRemove"); 

应改为阅读:

string vsql = string.Format("insert into Log values ({0}','{1}','{2}','{3}','{4}','{5}','{6}')",comboBox1.Text, comboBox2.Text, int.Parse(textBox1.Text), int.Parse(textBox1.Text), textBox2.Text, textBox3.Text, addRemove); 

我感动了双引号,可以从行都快结束了一点点{6}后。

的问题是书面,整个巨大的字符串正被作为唯一参数String.Format,这意味着没有论据可用于取代传递 - 因此错误。

我建议你改进代码格式,因为这种错误很容易被阻止。考虑这样的格式:

string vsql = string.Format(
    "insert into Log values ({0}','{1}','{2}','{3}','{4}','{5}','{6}')", 
    comboBox1.Text, 
    comboBox2.Text, 
    int.Parse(textBox1.Text), 
    int.Parse(textBox1.Text), 
    textBox2.Text, 
    textBox3.Text, 
    addRemove); 

这很容易看到这是怎么回事。

0

你的问题是在这里:

string vsql = string.Format("insert into Log values ({0}','{1}','{2}','{3}','{4}','{5}','{6}'),comboBox1.Text, comboBox2.Text, int.Parse(textBox1.Text), int.Parse(textBox1.Text), textBox2.Text, textBox3.Text, addRemove"); 

需要是这样的:

string vsql = string.Format("insert into Log values ({0}','{1}','{2}','{3}','{4}','{5}','{6}')",comboBox1.Text, comboBox2.Text, int.Parse(textBox1.Text), int.Parse(textBox1.Text), textBox2.Text, textBox3.Text, addRemove); 
相关问题