指定的演员阵容无效

发布于 2024-09-28 15:43:12 字数 3702 浏览 2 评论 0原文

            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() 。如果我在关闭前一个数据读取器后使用相同的命令获取另一个数据读取器,则会在“int q = (int)dbreader.GetValue(rni);”处抛出错误“指定的转换无效”。请帮助我...提前致谢

            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();

The problem here is that the statements in the while(dbreader.read()) are not executed, rather directly conn.Close() is performed. And if I take another datareader with same command after closing the previous datareader, the error "Specified cast not valid" is thrown at "int q = (int)dbreader.GetValue(rni);". Please help me out...thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

躲猫猫 2024-10-05 15:43:12

DataReader 是只向前的。你不能“重用”它。

您所做的就是根据记录数分配一个数组。首先运行 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();

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

关于正确释放资源还有更多要说的。最佳实践是将参数 CommandBehavior.CloseConnection 添加到 ExecuteReader,在 using() 构造中创建数据读取器并发出 cmd.Dispose()最后确保 SQL 连接资源得到正确释放。

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

DataReaders are forward-only. You can't "reuse" it.

All you are doing is allocating an array based on the number of records. Either run a COUNT query first to get the number of records, or re-allocate your array on the fly in the one loop.

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();

... carry on with your 2nd loop

There is more to say about releasing resources properly. Best practice is to add parameter CommandBehavior.CloseConnection to ExecuteReader, create your data reader inside a using() construct and issue cmd.Dispose() at the end to ensure SQL connection resources are released properly.

.. though actually since it's a local file you're using it may not matter so much but generally speaking you should do that. Otherwise its very easy to find that an orphaned DataReader has not released its connection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文