SqlDataReader使用

    SqlDataReader myReader = null;
    SqlConnection mySqlConnection = new SqlConnection("数据库连接");
    SqlCommand mySqlCommand = new SqlCommand("select * from customers", mySqlConnection);
    try
    {
      mySqlConnection.Open();
      myReader = mySqlCommand.ExecuteReader();
      while (myReader.Read())
      {
        Console.Write(myReader["CustomerID"].ToString() + "    ");
        Console.WriteLine(myReader["CompanyName"].ToString());
      }
    }
    catch(Exception e)
    {
      Console.WriteLine(e.ToString());
    }
    finally
    {
      if (myReader != null)
        myReader.Close();
      if (mySqlConnection.State == ConnectionState.Open)
        mySqlConnection.Close();
}
//操作这个对象
SqlConnection myConnection = new SqlConnection("server=(local)\NetSDK;Integrated Security=SSPI;database=northwind");
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter("select * from customers", myConnection);
mySqlDataAdapter.InsertCommand.CommandText = "sp_InsertCustomer";
mySqlDataAdapter.InsertCommand.CommandType = CommandType.StoredProcedure;
mySqlDataAdapter.Update(myDataSet);

mySqlDataAdapter->Update(myDataSet);

posted on 2006-07-24 12:05  欧阳  阅读(970)  评论(1编辑  收藏  举报