同时将记录插入访问数据库异常抛出c#

时间:2016-02-17 18:04:49

标签: 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,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_photo:

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("@pictureBox6", photo_aray);

中对象execption的实例

如果我在

之后放置了conv_photo方法
using (OleDbConnection conn = new OleDbConnection(SqlString))
            {

}

循环它给我初始化字符串的格式不符合从索引0开始的规范.execption(Argument exception waS unhandled)

没有得到我应该做的事。

1 个答案:

答案 0 :(得分:0)

这里有两个问题:

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

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

    并将cmd对象作为参数传递给它。

    1. 您的SqlString不是连接字符串。这是一个SQL语句。如果您不熟悉,您将需要查看connection string是什么。
相关问题