数据阅读器问题

时间:2010-10-17 09:51:20

标签: c# excel

  string Path = TextBox3.Text;
        // initialize the Excel Application class
        Excel.ApplicationClass app = new ApplicationClass();
        // create the workbook object by opening the excel file.
        Excel.Workbook workBook = app.Workbooks.Open(Path,
                                                     0,
                                                     true,
                                                     5,
                                                     "",
                                                     "",
                                                     true,
                                                     Excel.XlPlatform.xlWindows,
                                                     "\t",
                                                     false,
                                                     false,
                                                     0,
                                                     true,
                                                     1,
                                                     0);
        // get the active worksheet using sheet name or active sheet
        Excel.Worksheet workSheet = (Excel.Worksheet)workBook.ActiveSheet;
        int index = 2;
        // This row,column index should be changed as per your need.
        // i.e. which cell in the excel you are interesting to read.
        object rowIndex = (object)index;

        object colIndex1 = 1;
        object colIndex2 = 2;
        SqlDataReader reader;
        try
        {
            SqlConnection sqlConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Examination_Cell;Integrated Security=True");
            sqlConnection.Open();
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            string x = TextBox1.Text;
            string y = x.Substring(1, 1);
            string z = x.Substring(0, 1);


            TextBox4.Text = ("Roll No. \t Marks\n");
            string RollNo = "";
            string Marks = "";
            while (((Excel.Range)workSheet.Cells[rowIndex, colIndex1]).Value2 != null)
            {

                RollNo = ((Excel.Range)workSheet.Cells[rowIndex, colIndex1]).Value2.ToString();
                Marks = ((Excel.Range)workSheet.Cells[rowIndex, colIndex2]).Value2.ToString();
                TextBox4.Text += (RollNo + "     \t     " + Marks + "\n");

                index++;
                rowIndex = index;

                sqlCommand.CommandText = "select StudentID from Student where APID=" + int.Parse(y)+ "and Semester=" + int.Parse(z) + "and Roll_No=" + int.Parse(RollNo);
                reader = sqlCommand.ExecuteReader();
                while (reader.Read())
                {
                    // Console.WriteLine(reader["Group"]);
                    int a = (int)reader["StudentID"];
                    SqlCommand cmd2 = new SqlCommand();
                    cmd2.Connection = sqlConnection;
                    cmd2.CommandText = "insert into Result values('" + TextBox1.Text + "'," + int.Parse(TextBox2.Text) + "," + a + "," + RollNo + "," + Marks + ")";
                    cmd2.ExecuteNonQuery();

                }

            }
            //Console.ReadLine();



        sqlConnection.Close();
        }
        catch (Exception ex)
        {
            app.Quit();
            Console.WriteLine(ex.Message);
            Console.ReadLine();
        }
    }

这里,当我在“while(reader.Read())”处放置一个断点时,while循环只输入一次,之后它直接进入“catch”块。该程序实际上是从excel表中获取数据,从student表中获取StudentID,然后将数据插入另一个表“Result”。数据将从Excel工作表和Student表中获取,但不会插入到Result表中。还有一件事,我没有在Student和Result表之间给出任何关系。那么,是否可以或需要给予关系? 在此先感谢.........

1 个答案:

答案 0 :(得分:1)

我在代码示例中看不到对Reader.Read()的任何调用。

此外,我强烈建议您使用ADO.NET的最佳做法 - 将您的连接,命令和数据读取器包装到using() { ... }块中,以便在不再需要时自动释放它们。此外,您应该使用参数,而不是将SQL select语句串联在一起。

所以你的代码应该是这样的:

string query = "SELECT StudentID FROM Student WHERE APID = @APID AND Semester  = @Semester AND Roll_No = @RollNo";

using(SqlConnection _con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Examination_Cell;Integrated Security=True"))
using(SqlCommand _cmd = new SqlCommand(query, _con))
{
   _cmd.Parameters.Add("@APID", SqlDbType.Int).Value = ......;
   _cmd.Parameters.Add("@Semester", SqlDbType.Int).Value = ......;
   _cmd.Parameters.Add("@RollNo", SqlDbType.Int).Value = ......;

   _con.Open();
   using(SqlDataReader reader = _cmd.ExecuteReader())
   {
       while (reader.Read())
       {
          // do something
       }
   }

   _con.Close();
}