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

时间:2011-01-17 21:14:41

标签: c# asp.net

我试图使用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 {“参数化查询”(@CaseID int,@ ExhibitType ntext,@ ExhibitImage图像,@ DateReceive'需要参数'@SuspectID',这是未提供的。“} System.Exception {System.Data。 SqlClient.SqlException}
  •   

2 个答案:

答案 0 :(得分:4)

如果要传递NULL数据库/参数类型,则需要使用DBNull.Value,如下所示:

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

现在,无论你在什么地方设置null,都要这样做,我很确定它会正常工作。一切都很好看。

答案 1 :(得分:0)

你可以更容易地做到这一点,试试这样:

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);
                command.Parameters.AddWithValue("@CaseID", caseid);
                //and so on for your 10 parameters




                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>";


               }
}

我不确定这是否会在不知道确切异常的情况下实际修复正在发生的事情。如果你能提供有用的实际例外。