指定演员表无效

时间:2010-10-21 17:57:06

标签: oledbdatareader

            SqlDataReader reader;
            string r="C:\\Users\\Shivam\\Documents\\";
            if ((FileUpload1.PostedFile != null)&&(FileUpload1.PostedFile.ContentLength > 0))
            {
                r += System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);

            }

            OleDbConnection oconn =
              new OleDbConnection
                (@"Provider=Microsoft.Jet.OLEDB.4.0;"
                 + "Data Source="+r+";"
                 + @"Extended Properties=""Excel 8.0;HDR=Yes;""");
            oconn.Open();
            conn.Open();
            OleDbCommand dbcom = new OleDbCommand("SELECT * FROM [Sheet1$]", oconn);
            OleDbDataReader dbreader = dbcom.ExecuteReader();

            //dbread = dbreader;
            int rni = dbreader.GetOrdinal ("RollNo");
            int mki = dbreader.GetOrdinal ("marks");
            int rowcount =0;
            while(dbreader.Read())
            { rowcount++; }
            //dbreader.Close();
            //OleDbDataReader dbread = dbcom.ExecuteReader();
            int[] rn = new int[rowcount];
            int[] gr = new int[rowcount];


            while (dbreader.Read())
            {

                int o = 0;
                for(int i=0;i<rowcount;i++)
                {
                    int q = (int)dbreader.GetValue(rni);
                    int p = (int)dbreader.GetValue(mki);
                    rn[i] = q;
                    gr[i] = p;
                    //roll[i] = valid(odr, 0);//Here we are calling the valid method
                    //marks[i] = valid(odr, 1);
                    //i++;
                    if (gr[i] >= 11)
                    { o=i; }
                }
                if(o!=0)
                { break; }
                TextBox4.Text += rn + "\t" + gr;

                //Here using this method we are inserting the data into the database

                x = TextBox2.Text.Substring(0, 1);
                y = TextBox2.Text.Substring(1, 1);

                //for (int s = 0; s < roll.Length; s++)
                //{

                    //SqlDataAdapter sda = new SqlDataAdapter("select StudentID from Student where APID=" + int.Parse(y)+ "and Semester=" + int.Parse(z) + "and Roll_No=" + int.Parse(RollNo), conn);
                    //DataSet ds = new DataSet();
                    //sda.Fill(ds);
                    //GridView1.DataSource = ds;
                    //GridView1.DataBind();
                    SqlCommand command = new SqlCommand();
                    command.Connection = conn;
                    //command.Connection.Open();
                    command.CommandType = CommandType.Text;
                    int c = rn.Length;
                    for (int n = 0; n<rn.Length; n++)
                    {
                        command.CommandText = "Select StudentID from Student where APID=" + int.Parse(x) + "and Semester=" + int.Parse(y) + "and Roll_No=" + rn[n];

                    }
                reader = command.ExecuteReader();

                    while (reader.Read())
                    {
                        TextBox4.Text = reader.GetInt32(0).ToString();
                        a = (int)reader["StudentID"];
                        for (int v = 0; v < rn.Length; v++)
                        {
                            insertdataintosql(rn[v], gr[v]);
                        }
                    }
                //}

            }
            conn.Close();
            oconn.Close();

这里的问题是while(dbreader.read())中的语句不会被执行,而是直接执行conn.Close()。如果在关闭前一个datareader之后我使用相同命令的另一个datareader,则会在“int q =(int)dbreader.GetValue(rni);”中抛出错误“Specified cast not valid”。请帮帮我......提前谢谢

1 个答案:

答案 0 :(得分:0)

DataReaders是仅向前的。你不能“重用”它。

您所做的就是根据记录数分配数组。首先运行COUNT查询以获取记录数,或者在一个循环中动态重新分配数组。

OleDbCommand dbcom = new OleDbCommand("SELECT COUNT(*) as RowCount FROM [Sheet1$]", oconn);
 dbreader = dbcom.ExecuteReader();
dbreader.Read();
rowcount = dbreader.GetOrdinal("RowCount");
dbcom.Close();

dbcom = new OleDbCommand("SELECT * FROM [Sheet1$]", oconn);
 dbreader = dbcom.ExecuteReader();

...继续你的第二个循环

关于正确释放资源还有很多话要说。最佳做法是向ExecuteReader添加参数CommandBehavior.CloseConnection,在using()构造内创建数据读取器,并在末尾发出cmd.Dispose()以确保正确释放SQL连接资源。

..虽然实际上因为它是你使用它的本地文件可能并不重要,但一般来说你应该这样做。否则很容易发现孤立的DataReader没有释放它的连接。