阅读背景:

我的代码给了我一个错误叫做"已经有一个开放的DataReader与这个命令相关联它必须先关闭"

来源:互联网 
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = "Data Source=PROMOD-PC;Initial Catalog=travel_Directions;Integrated Security=True";

    String Strt_Address = TextBox1.Text;
    String End_Address = TextBox2.Text;

    String filePath = FileUpload1.PostedFile.FileName;
    String filename = Path.GetFileName(filePath);
    String ExtStr = Path.GetExtension(filename);
    String contenttype = String.Empty;

    switch (ExtStr)
    {
        case ".png":
            contenttype = "image/png";
            break;

        case ".jpg":
            contenttype = "image/jpg";
            break;

        case ".gif":
            contenttype = "image/gif";
            break;
    }

    if (contenttype != string.Empty)
    {
        Stream Strmf = FileUpload1.PostedFile.InputStream;
        BinaryReader br = new BinaryReader(Strmf);
        Byte[] imgbytes = br.ReadBytes((Int32)Strmf.Length);


        //SqlDataReader sqldread = new SqlDataReader();

        String selQuery = "SELECT Id FROM MapDataImage WHERE Source='" + TextBox1.Text + "';";
        {

            SqlCommand scmd = new SqlCommand(selQuery, conn);

            conn.Open();

            SqlDataReader sqldread = scmd.ExecuteReader();

            while (sqldread.Read())
            {
                int Dbid = (int)sqldread["Id"];
                //string DbId = sqldread.GetInt32("Id").ToString();

                Label4.Text = Convert.ToString(Dbid);

                String QueryStr = "INSERT INTO User_Images(Id,Image) VALUES ('" + Dbid + "',@Image)";
                SqlCommand scmd1 = new SqlCommand(QueryStr, conn);
                scmd1.Parameters.Add("@Image", SqlDbType.VarBinary).Value = imgbytes;
                scmd1.ExecuteNonQuery();
            }


            //String QueryStr = "UPDATE MapDataImage SET Image = @Image WHERE Source='" + TextBox1.Text + "';";
            //SqlCommand scmd = new SqlCommand(QueryStr, conn);
            //scmd.Parameters.Add("@Image", SqlDbType.VarBinary).Value = imgbytes;
            sqldread.Close();
            conn.Close();
        }
 }
    SqlConnection conn = new SqlConnection();
 



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: