2010-10-21 46 views
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);」處拋出錯誤「Specified cast not valid」。請幫助我...提前致謝指定的演員表無效

回答

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

...進行與第二環

還有更多的說的正確釋放資源。最佳做法是將參數CommandBehavior.CloseConnection添加到ExecuteReader,在using()構造中創建數據讀取器,並在末尾發出cmd.Dispose()以確保SQL連接資源正確釋放。

..雖然實際上,因爲它是一個本地文件,你使用它可能並不重要,但一般來說你應該這樣做。否則很容易發現孤立的DataReader沒有釋放它的連接。

+0

嘿你可以ü請寫代碼片段...我無法得到笏你想說... – shivam 2010-10-21 18:09:40

+0

更新以上... – 2010-10-21 18:19:53