C# 将记录插入access db时,c中会抛出异常#

C# 将记录插入access db时,c中会抛出异常#,c#,ms-access-2007,C#,Ms Access 2007,iam正在尝试将一条记录插入ms access数据库,以供下面的代码和方法使用 string SqlString = "Insert Into RegistrationForm (ClientCount,Name,Address,Contact,Documents,Money_Taking_Date,Muddat,Money_Return_date,Account_status,Taking_Amout,Interest_per_month,Pending_interest_month,Pend

iam正在尝试将一条记录插入ms access数据库,以供下面的代码和方法使用

string SqlString = "Insert Into RegistrationForm (ClientCount,Name,Address,Contact,Documents,Money_Taking_Date,Muddat,Money_Return_date,Account_status,Taking_Amout,Interest_per_month,Pending_interest_month,Pending_interst_Amount,Total_Amount,Client_image,Document_image1,Document_image2) Values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            conv_photo();
            using (OleDbConnection conn = new OleDbConnection(SqlString))
            {

                using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
                {

                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("ClientCount", lblcount.Text);
                    cmd.Parameters.AddWithValue("Name", textBox20.Text);
                    cmd.Parameters.AddWithValue("Address", textBox21.Text);
                    cmd.Parameters.AddWithValue("Contact", textBox19.Text);
                    cmd.Parameters.AddWithValue("Documents", textBox18.Text);
                    cmd.Parameters.AddWithValue("Money_Taking_Date", maskedTextBox1.Text.ToString());
                    cmd.Parameters.AddWithValue("Muddat", textBox22.Text);
                    cmd.Parameters.AddWithValue("Money_Return_date", maskedTextBox2.Text.ToString());
                    cmd.Parameters.AddWithValue("Account_status", textBox23.Text);
                    cmd.Parameters.AddWithValue("Taking_Amout", textBox17.Text);
                    cmd.Parameters.AddWithValue("Interest_per_month", textBox16.Text);
                    cmd.Parameters.AddWithValue("Pending_interest_month", textBox15.Text);
                    cmd.Parameters.AddWithValue("Pending_interst_Amount", Convert.ToDouble(textBox13.Text));
                    cmd.Parameters.AddWithValue("Total_Amount", Convert.ToDouble(textBox14.Text));
                    cmd.Parameters.AddWithValue("@Client_image", pictureBox6);
                    cmd.Parameters.AddWithValue("Document_image1", pictureBox4);
                    cmd.Parameters.AddWithValue("Document_image2", pictureBox5);

                    conn.Open();
                    int n=cmd.ExecuteNonQuery();
                    conn.Close();
                    if (n > 0)
                    {
                        MessageBox.Show("record inserted");
                        loaddata();
                       // rno++;
                    }
                    else
                        MessageBox.Show("insertion failed");
                }
            }
方法conv_照片:

public void conv_photo()
         {
             //converting photo to binary data


             if (pictureBox6.Image != null)
             {
                 //using MemoryStream:
                 ms = new MemoryStream();
                 pictureBox6.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                 byte[] photo_aray = new byte[ms.Length];
                 ms.Position = 0;
                 ms.Read(photo_aray, 0, photo_aray.Length);
                 cmd.Parameters.AddWithValue("@pictureBox6", photo_aray);
             }

             if (pictureBox4.Image != null)
             {
                 //using MemoryStream:
                 ms = new MemoryStream();
                 pictureBox4.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                 byte[] photo_aray = new byte[ms.Length];
                 ms.Position = 0;
                 ms.Read(photo_aray, 0, photo_aray.Length);
                 cmd.Parameters.AddWithValue("@pictureBox4", photo_aray);
             }
             if (pictureBox5.Image != null)
             {
                 //using MemoryStream:
                 ms = new MemoryStream();
                 pictureBox5.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                 byte[] photo_aray = new byte[ms.Length];
                 ms.Position = 0;
                 ms.Read(photo_aray, 0, photo_aray.Length);
                 cmd.Parameters.AddWithValue("@pictureBox5", photo_aray);
             }
现在的问题是当我运行应用程序时

对象引用未设置为
cmd.Parameters.AddWithValue(“@picturebx6”,photo_aray”)中对象执行选项抛出的实例

如果我把conv_photo方法放在

using (OleDbConnection conn = new OleDbConnection(SqlString))
            {

}
循环它给我初始化字符串的格式不符合从索引0开始的规范。执行选项(未处理参数异常)

没有得到我应该做什么。

这里有两个问题:

  • 您似乎正在使用两个版本的
    cmd
    。其中一个似乎是全局的,因为它在conv_photo中可用,而另一个则在top方法所在的任何地方实例化。全局对象根本没有被实例化(根据您显示的内容)
  • 您的using语句创建了一个全新的
    cmd
    对象,该对象与您试图在conv_照片中使用的对象无关。摆脱全局cmd对象,并将对conv_photo的调用放在

    using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
    
    并将
    cmd
    对象作为参数传递给它

  • 您的SqlString不是连接字符串。这是一个SQL语句。如果你不熟悉a,你需要了解a是什么

  • 只是为了给初学者澄清一点;您需要两个不同的字符串来与C#中的数据存储交互。传递给连接构造函数的字符串是连接字符串。它是一个包含特定格式数据的字符串,用于指示ADO.Net如何连接到数据库。传递给命令的字符串应该是SQL语句。这是一个字符串,包含要对数据库执行的SQL语言语句。