gpt4 book ai didi

c# - 使用参数在 C# 中插入查询

转载 作者:行者123 更新时间:2023-11-30 21:17:33 25 4
gpt4 key购买 nike

我正在尝试使用以下来自 C# 的插入查询将多个列插入到我的数据库中,但它以某种方式在某处抛出异常,我猜测没有提供用于插入的值。我只想确认这一点并了解如何修复插入语句。我在下面有一张图片,显示了在运行时传递给参数的内容。我使用断点来获取此信息。

需要你在这方面的专业知识......谢谢

if (Page.IsValid)
{
DateTime exhibitDate = DateTime.Now;
int caseid = Convert.ToInt32(CaseIDDropDownList.SelectedItem.Text);
string exhibittype = exhibitTypeTextBox.Text.ToString();
string storedloc = storedLocationTextBox.Text.ToString();
string offid = DropDownList1.SelectedItem.Text.ToString();
Stream imgStream = exhibitImageFileUpload.PostedFile.InputStream;
int imgLen = exhibitImageFileUpload.PostedFile.ContentLength;
byte[] imgBinaryData = new byte[imgLen];
int n = imgStream.Read(imgBinaryData,0,imgLen);
try
{
SqlConnection connections = new SqlConnection(strConn);
SqlCommand command = new SqlCommand("INSERT INTO Exhibits (CaseID, ExhibitType, ExhibitImage, DateReceived, StoredLocation, InvestigationStatus, OfficerID, SuspectID, InvestigatorID, ManagerID, AdminID ) VALUES (@CaseID, @ExhibitType, @ExhibitImage, @DateReceived, @StoredLocation, @InvestigationStatus, @OfficerID, @SuspectID, @InvestigatorID, @ManagerID, @AdminID)", connections);

SqlParameter param0 = new SqlParameter("@CaseID", SqlDbType.Int);
param0.Value = caseid;
command.Parameters.Add(param0);

SqlParameter param1 = new SqlParameter("@ExhibitType", SqlDbType.NText);
param1.Value = exhibittype;
command.Parameters.Add(param1);

SqlParameter param2 = new SqlParameter("@ExhibitImage", SqlDbType.Image);
param2.Value = imgBinaryData;
command.Parameters.Add(param2);

SqlParameter param3 = new SqlParameter("@DateReceived", SqlDbType.SmallDateTime);
param3.Value = exhibitDate;
command.Parameters.Add(param3);

SqlParameter param4 = new SqlParameter("@StoredLocation", SqlDbType.NText);
param4.Value = storedloc;
command.Parameters.Add(param4);

SqlParameter param5 = new SqlParameter("@InvestigationStatus", SqlDbType.VarChar, 50);
param5.Value = "";
command.Parameters.Add(param5);

SqlParameter param6 = new SqlParameter("@OfficerID", SqlDbType.NChar, 10);
param6.Value = offid;
command.Parameters.Add(param6);

SqlParameter param7 = new SqlParameter("@SuspectID", SqlDbType.NChar, 10);
param7.Value = null;
command.Parameters.Add(param7);

SqlParameter param8 = new SqlParameter("@InvestigatorID", SqlDbType.NChar, 10);
param8.Value = null;
command.Parameters.Add(param8);

SqlParameter param9 = new SqlParameter("@ManagerID", SqlDbType.NChar, 10);
param9.Value = null;
command.Parameters.Add(param9);

SqlParameter param10 = new SqlParameter("@AdminID", SqlDbType.NChar, 10);
param10.Value = adminID;
command.Parameters.Add(param10);

connections.Open();
int numRowsAffected = command.ExecuteNonQuery();
connections.Close();

if (numRowsAffected != 0)
{
Response.Write("<BR>Rows Inserted successfully");
CaseIDDropDownList.ClearSelection();
exhibitTypeTextBox.Text = null;
storedLocationTextBox.Text = null;
DropDownList1.ClearSelection();
}
else
{
Response.Write("<BR>An error occurred uploading the image");
}
}
catch (Exception ex)
{
string script = "<script>alert('" + ex.Message + "');</script>";
}

alt text

异常情况如下

  • $exception {"The parameterized query '(@CaseID int,@ExhibitType ntext,@ExhibitImage image,@DateReceive' expects the parameter '@SuspectID', which was not supplied."} System.Exception {System.Data.SqlClient.SqlException}

最佳答案

如果你想为你的数据库/参数类型传入NULL,你需要像这样使用DBNull.Value:

SqlParameter param9 = new SqlParameter("@ManagerID", SqlDbType.NChar, 10);
param9.Value = DBNull.Value;
command.Parameters.Add(param9);

无论您现在将什么设置为 null,都可以执行此操作,我很确定它会正常工作。一切看起来都很好。

关于c# - 使用参数在 C# 中插入查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4717961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com