将记录插入到Access DB中的异常时抛出C# [英] While inserting the record into access db exception throw in c#
问题描述
iam试图将以下代码和方法使用的记录插入ms-access数据库中
我尝试过的事情:
string SqlString =插入注册表单(ClientCount,名称,地址,联系人,文档,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,图像?)? ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);
conv_photo();
使用(OleDbConnection conn = new OleDbConnection(SqlString))
{
使用(OleDbCommand cmd =新的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();
如果(n> 0)
{
MessageBox.Show(已插入记录");
loaddata();
//rno ++;
}
其他
MessageBox.Show(插入失败");
}
}
iam尝试将以下代码和方法使用的记录插入ms-access数据库中
string SqlString =插入RegistrationForm(ClientCount,名称,地址,联系人,文档,Money_Taking_Date,Muddat,
Money_Return_date,
Account_status,Taking_Amout,Interest_per_month,Pending_interest_month,
Pending_interst_Amount,
总计金额,
Client_image,
Document_image1,Document_image2)值(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?));
conv_photo();
使用(OleDbConnection conn = new OleDbConnection(SqlString))
{
使用(OleDbCommand cmd =新的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();
如果(n> 0)
{
MessageBox.Show(已插入记录");
loaddata();
//rno ++;
}
其他
MessageBox.Show(插入失败");
}
}
方法conv_photo:
公共无效conv_photo()
{
//将照片转换为二进制数据
如果(pictureBox6.Image!= null)
{
//使用MemoryStream:
ms =新的MemoryStream();
pictureBox6.Image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
byte [] photo_aray =新的byte [ms.Length];
ms.Position = 0;
ms.Read(photo_aray,0,photo_aray.Length);
cmd.Parameters.AddWithValue("@ pictureBox6",photo_aray);
}
如果(pictureBox4.Image!= null)
{
//使用MemoryStream:
ms =新的MemoryStream();
pictureBox4.Image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
byte [] photo_aray =新的byte [ms.Length];
ms.Position = 0;
ms.Read(photo_aray,0,photo_aray.Length);
cmd.Parameters.AddWithValue("@ pictureBox4",photo_aray);
}
如果(pictureBox5.Image!= null)
{
//使用MemoryStream:
ms =新的MemoryStream();
pictureBox5.Image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
byte [] photo_aray =新的byte [ms.Length];
ms.Position = 0;
ms.Read(photo_aray,0,photo_aray.Length);
cmd.Parameters.AddWithValue("@ pictureBox5",photo_aray);
}
现在出现问题是 何时,我运行该应用程序
对象引用未设置到对象的实例执行 throw in cmd.Parameters.AddWithValue(" @ pictureBox6",photo_aray);线
如果,我在之后放置了conv_photo方法
使用(OleDbConnection conn = 新 OleDbConnection(SqlString))
{
}
循环给我,初始化字符串的格式不符合从索引 0 开始的规范.执行(未处理参数异常)
没有得到我应该怎么做 .
第一个问题:
SqlString
变量包含要执行的查询.传递给OleDbConnection
构造函数的参数必须是数据库的连接字符串.
访问连接字符串-ConnectionStrings.com [ ^ ]
使用(OleDbConnection conn = 新 OleDbConnection( Provider = Microsoft.ACE.OLEDB.12.0;数据源= | DataDirectory | \ YourDatabase.accdb;持久安全信息= False;"跨度>)) { ... }
第二个问题:
您正在尝试通过conv_photo
方法向命令中添加参数,但是cmd
对象是另一种方法中的局部变量.您需要将该变量作为参数传递给conv_photo
方法:
公共 void conv_photo(OleDbCommand cmd) { ... } ... 使用(OleDbCommand cmd = 新 OleDbCommand(SqlString,conn)) { ... conv_photo(cmd); ... }
第三个问题:
您的查询需要16个参数.您的方法已经向集合中添加了17个参数,而您的conv_photo
方法将添加另外三个参数.
您需要修复查询,以使添加的参数数量与查询期望的参数数量匹配.您已在查询的第一部分中指定了17列,因此在VALUES
部分中需要有17个?
占位符.
您还需要修复试图将控件作为值传递的三个参数.你不能那样做;您需要传递数据库可以理解的适当值.
// 这些参数将不起作用: cmd.Parameters.AddWithValue(" ,pictureBox6); cmd.Parameters.AddWithValue(" ,pictureBox4); cmd.Parameters.AddWithValue(" ,pictureBox5);
实例化的OleDbCommand对象cmd
使用(OleDbCommand cmd = 新 OleDbCommand(SqlString,conn))
与您的方法conv_photo()
中的不同.cmd
变量可能存在未初始化的类变量(空).
您应该删除cmd
作为类变量,为conv_photo()
方法提供一个OleDbCommand cmd
参数,并像这样调用它:
使用(OleDbCommand cmd = 新 OleDbCommand(SqlString,conn)) { conv_photo(cmd); // ... }
并且,由于conv_photo(..)
已经添加了图像字节数组的参数,因此请从第一种方法中删除根据cmd.Parameters.AddWithValue(..)
的三个参数.并在这些if (pictureBox.Image != null)
-语句中添加一个else块,以便在图像为null时也添加一个参数(值为null
),否则由于参数太少会出现错误. br/>
除此之外:还可以像使用OleDbConnection-和OleDbCommand-objects一样,在using-blocks中使用MemoryStreams:
使用( var ms = new MemoryStream()) { // ... }
(并删除ms
类变量.)
这两种解决方案对我都很有用,
iam trying to insert a record into ms-access database for that below code and method use
What I have tried:
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");
}
}
iam trying to insert a record into ms-access database for that below code and method use
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");
}
}
method 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);
}
now problem is when i run the application Object reference not set to an instance of an object execption throw in cmd.Parameters.AddWithValue("@pictureBox6", photo_aray); line if i put conv_photo method after using (OleDbConnection conn = new OleDbConnection(SqlString)) { } loop it giving me Format of the initialization string does not conform to specification starting at index 0. execption (Argument exception waS unhandled) not getting what should i do .解决方案First problem:
TheSqlString
variable contains the query you want to execute. The parameter you pass to theOleDbConnection
constructor needs to be the connection string for your database.
Access connection strings - ConnectionStrings.com[^]
using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=|DataDirectory|\YourDatabase.accdb; Persist Security Info=False;")) { ... }
Second problem:
You''re trying to add parameters to the command from theconv_photo
method, but thecmd
object is a local variable in a different method. You need to pass that variable to theconv_photo
method as a parameter:
public void conv_photo(OleDbCommand cmd) { ... } ... using (OleDbCommand cmd = new OleDbCommand(SqlString, conn)) { ... conv_photo(cmd); ... }
Third problem:
Your query requires 16 parameters. Your method has already added 17 parameters to the collection, and yourconv_photo
method is going to add another three parameters.
You need to fix your query so that the number of parameters you add matches the number of parameters your query is expecting. You''ve specified 17 columns in the first part of the query, so you need to have 17?
placeholders in theVALUES
part.
You also need to fix the three parameters which try to pass a control as the value. You can''t do that; you need to pass a suitable value which the database understands instead.
// These parameters will not work: cmd.Parameters.AddWithValue("@Client_image", pictureBox6); cmd.Parameters.AddWithValue("Document_image1", pictureBox4); cmd.Parameters.AddWithValue("Document_image2", pictureBox5);
The OleDbCommand-objectcmd
that your instantiating with
using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
isn''t the same as in your methodconv_photo()
. Thecmd
-variable there is probably a not initialized class variable (null).
You should removecmd
as class-variable, give theconv_photo()
method aOleDbCommand cmd
argument instead and call it like this:
using (OleDbCommand cmd = new OleDbCommand(SqlString, conn)) { conv_photo(cmd); // ... }
And asconv_photo(..)
is already adding the parameters for the image-byte-arrays, remove the three accordingcmd.Parameters.AddWithValue(..)
from the first method. And add an else-block to thoseif (pictureBox.Image != null)
-statements in order to also add a parameter (with a value ofnull
) if the images are null, otherwise you''ll get an error because of too few parameters.
Apart from that: Also use the MemoryStreams in using-blocks, just like you did with the OleDbConnection- and OleDbCommand-objects:
using (var ms = new MemoryStream()) { // ... }
(And remove thems
class variable.)
both solution are helpful for me thnx guys its working fine
这篇关于将记录插入到Access DB中的异常时抛出C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!