Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用参数在C#中插入查询_C#_Asp.net - Fatal编程技术网

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

使用参数在C#中插入查询,c#,asp.net,C#,Asp.net,我试图使用下面的C#insert查询将几列插入到我的数据库中,但不知为什么它会在某个地方抛出异常,我猜没有为插入提供值。我只是想确认一下,看看如何修复insert语句。下面的图片显示了在运行时传递到参数中的内容。我用了一个断点来获取这个信息 需要你在这方面的专业知识…谢谢 if (Page.IsValid) { DateTime exhibitDate = DateTime.Now; int caseid = Convert.ToI

我试图使用下面的C#insert查询将几列插入到我的数据库中,但不知为什么它会在某个地方抛出异常,我猜没有为插入提供值。我只是想确认一下,看看如何修复insert语句。下面的图片显示了在运行时传递到参数中的内容。我用了一个断点来获取这个信息

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

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>";
            }
if(Page.IsValid)
{
DateTime exhibitDate=DateTime.Now;
int caseid=Convert.ToInt32(CaseIDDropDownList.SelectedItem.Text);
字符串exhibittype=exhibitTypeTextBox.Text.ToString();
string storedloc=storedLocationTextBox.Text.ToString();
string offid=DropDownList1.SelectedItem.Text.ToString();
Stream imgStream=explamagefileupload.PostedFile.InputStream;
int imgLen=exbamagefileupload.PostedFile.ContentLength;
字节[]imgBinaryData=新字节[imgLen];
int n=imgStream.Read(imgBinaryData,0,imgLen);
尝试
{
SqlConnection连接=新的SqlConnection(strConn);
SqlCommand command=新的SqlCommand(“插入展品(案例ID、展览类型、展览图像、接收日期、存储位置、调查状态、OfficeId、怀疑、调查员ID、ManagerID、AdminID)”值(@CaseID、@exhibitype、@exhimage、@DateReceived、@StoredLocation、@InvestigationStatus、@OfficerID、@SuspectID、@investorid、@ManagerID、@AdminID)”,连接);
SqlParameter param0=新的SqlParameter(“@CaseID”,SqlDbType.Int);
param0.Value=caseid;
command.Parameters.Add(param0);
SqlParameter param1=新的SqlParameter(“@ExhibitType”,SqlDbType.NText);
参数1.值=参展商类型;
command.Parameters.Add(param1);
SqlParameter param2=新的SqlParameter(“@exbamage”,SqlDbType.Image);
param2.Value=imgBinaryData;
命令.Parameters.Add(param2);
SqlParameter param3=新的SqlParameter(“@DateReceived”,SqlDbType.SmallDateTime);
参数3.值=参展商日期;
command.Parameters.Add(param3);
SqlParameter param4=新的SqlParameter(“@StoredLocation”,SqlDbType.NText);
param4.Value=storedloc;
命令.Parameters.Add(param4);
SqlParameter param5=新的SqlParameter(“@InvestigationStatus”,SqlDbType.VarChar,50);
参数5.值=”;
命令.Parameters.Add(param5);
SqlParameter param6=新的SqlParameter(“@OfficeId”,SqlDbType.NChar,10);
param6.值=offid;
命令.Parameters.Add(param6);
SqlParameter param7=新的SqlParameter(“@SuspectID”,SqlDbType.NChar,10);
param7.Value=null;
命令.Parameters.Add(param7);
SqlParameter param8=新的SqlParameter(“@investorId”,SqlDbType.NChar,10);
param8.Value=null;
命令.Parameters.Add(param8);
SqlParameter param9=新的SqlParameter(“@ManagerID”,SqlDbType.NChar,10);
param9.Value=null;
命令.Parameters.Add(param9);
SqlParameter param10=新的SqlParameter(“@AdminID”,SqlDbType.NChar,10);
param10.Value=adminID;
命令.Parameters.Add(param10);
连接。打开();
int numrowsimpacted=command.ExecuteNonQuery();
连接。关闭();
如果(受影响的数值!=0)
{
写入(
行插入成功); CaseIDDropDownList.ClearSelection(); exhibitTypeTextBox.Text=null; storedLocationTextBox.Text=null; DropDownList1.ClearSelection(); } 其他的 { Write(
上传图像时出错); } } 捕获(例外情况除外) { string script=“警报(“+ex.Message+”);”; }

例外情况如下

  • $exception{“参数化查询”(@CaseID int、@ExhibitType ntext、@exbamage image、@DateReceive'需要未提供的参数“@SuspectID”。}System.exception{System.Data.SqlClient.SqlException}

您可以更轻松地执行此操作,可以这样尝试:

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


               }
}
if(Page.IsValid)
{
DateTime exhibitDate=DateTime.Now;
int caseid=Convert.ToInt32(CaseIDDropDownList.SelectedItem.Text);
字符串exhibittype=exhibitTypeTextBox.Text.ToString();
string storedloc=storedLocationTextBox.Text.ToString();
string offid=DropDownList1.SelectedItem.Text.ToString();
Stream imgStream=explamagefileupload.PostedFile.InputStream;
int imgLen=exbamagefileupload.PostedFile.ContentLength;
字节[]imgBinaryData=新字节[imgLen];
int n=imgStream.Read(imgBinaryData,0,imgLen);
尝试
{
SqlConnection连接=新的SqlConnection(strConn);
SqlCommand command=新的SqlCommand(“插入展品(案例ID、展览类型、展览图像、接收日期、存储位置、调查状态、OfficeId、怀疑、调查员ID、ManagerID、AdminID)”值(@CaseID、@exhibitype、@exhimage、@DateReceived、@StoredLocation、@InvestigationStatus、@OfficerID、@SuspectID、@investorid、@ManagerID、@AdminID)”,连接);
command.Parameters.AddWithValue(“@CaseID”,CaseID);
//a
SqlParameter param9 = new SqlParameter("@ManagerID", SqlDbType.NChar, 10);
param9.Value = DBNull.Value;
command.Parameters.Add(param9);