0

我是 C# 的新手。请协助!

尝试在 MS 访问中将值插入我的数据库时出现错误错误消息是:索引(从零开始)必须大于或等于零且小于参数列表的大小。

下面是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace AzureSecureStore
{
    public partial class Client : Form
    {
        OleDbConnection vcon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\SB18\Documents\Visual Studio 2010\Projects\AzureSecureStore\AzureSecureStore\AzcureSecureStore Database.accdb");
        public Client()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {

        }

        private void Client_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'azcureSecureStore_DatabaseDataSet5.Client' table. You can move, or remove it, as needed.
            this.clientTableAdapter.Fill(this.azcureSecureStore_DatabaseDataSet5.Client);
            // TODO: This line of code loads data into the 'azcureSecureStore_DatabaseDataSet2.Client' table. You can move, or remove it, as needed.
            //this.clientTableAdapter.Fill(this.azcureSecureStore_DatabaseDataSet2.Client);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string ab = string.Format("insert into Client values({0}, '{1}', '{2}', {3}, {4}, '{5}', {6})", textBox1.Text, textBox2.Text, int.Parse(textBox3.Text), int.Parse(textBox4.Text), textBox9.Text, int.Parse(textBox10.Text));
            OleDbCommand vcom = new OleDbCommand(ab, vcon);
            vcom.ExecuteNonQuery();
            MessageBox.Show("Data stored successfully");
            vcom.Dispose();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
4

3 回答 3

0

您在这一行中有一个太多的参数占位符:

string ab = string.Format("insert into Client values({0}, '{1}', '{2}', {3}, {4}, '{5}', {6})",
    textBox1.Text, textBox2.Text, int.Parse(textBox3.Text), int.Parse(textBox4.Text), textBox9.Text, int.Parse(textBox10.Text));

您有 7 个占位符,但有 6 个参数。尝试更改为:

string ab = string.Format("insert into Client values({0}, '{1}', '{2}', {3}, {4}, '{5}')",
    textBox1.Text, textBox2.Text, int.Parse(textBox3.Text), int.Parse(textBox4.Text), textBox9.Text, int.Parse(textBox10.Text));
于 2013-01-12T07:02:11.637 回答
0

你有没有像这样改变你的连接字符串

OleDbConnection vcon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\SB18\Documents\Visual Studio 2010\Projects\AzureSecureStore\AzureSecureStore\AzcureSecureStore Database.accdb; Persist Security Info=False;");
于 2013-01-12T06:57:51.080 回答
0

你应该在你的按钮点击事件中尝试这样

OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;" & _

        "Data Source=C:\Database\RAAFeedback.mdb")

conn.Open();

OleDbCommand cmd = new OleDbCommand();

cmd.Connection = connection;

cmd.CommandText = "INSERT INTO Feedback([FirstName],[LastName],[Email],[Priphone],[Secphone],[Comment]) & _

VALUES (@FirstName,@LastName,@Priphone,@Secphone,@Comment)";

cmd.Parameters.Add("@FirstName", OleDbType.VarChar).value = FirstName;

cmd.Parameters.Add("@LastName", OleDbType.VarChar).value = LastName;

cmd.Parameters.Add("@Email", OleDbType.VarChar).value = Email;

cmd.Parameters.Add("@Priphone", OleDbType.VarChar).value = Priphone;

cmd.Parameters.Add("@Secphone", OleDbType.VarChar).value = Secphone;

cmd.Parameters.Add("@Comment", OleDbType.VarChar).value = Comment;

cmd.ExecuteNonQuery();

conn.Close();



'Clear the text boxes after a new insert

        FirstName.Text = ""

        LastName.Text = ""

        Email.Text = ""

        Priphone.Text = ""

        Secphone.Text = ""

        Comment.Text = ""
于 2013-01-12T06:52:16.897 回答