>我正在尝试将记录插入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);
行中抛出的对象执行实例
如果我把conv_photo方法放在之后
using (OleDbConnection conn = new OleDbConnection(SqlString))
{
}
循环它给我初始化字符串的格式不符合从索引 0 开始的规范。 执行(参数异常 waS 未处理(
不明白我该怎么办.
这里有两个问题:
- 您似乎正在使用两个版本的
cmd
。 一个似乎是全局的,因为它在 conv_photo 中可用,另一个在 top 方法所在的任何位置内实例化。 全局的根本没有被实例化(根据你显示的内容(。
您的 using 语句创建一个全新的 cmd
对象,该对象与您尝试在 conv_photo 中使用的对象无关。 摆脱全局cmd对象并将调用conv_photo放入您的内部
using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
并将cmd
对象作为参数传递到其中。
- 您的 SqlString 不是连接字符串。 它是一个 SQL 语句。 如果您不熟悉,则需要了解什么是连接字符串。