Tuesday, July 31, 2012

SqlCommand using example

using System.Data.SqlClient;
namespace OurApp.Models
{
   public static class HtmlTable
   {
      public static string Render()
      {
         string[] model = new string[] { "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
               "" };
         string connection_string =
               System.Configuration.ConfigurationManager.ConnectionStrings["baseDB"]
               .ConnectionString;
         
         using (SqlConnection con = new SqlConnection(connection_string))
         {
            SqlCommand cmd = new SqlCommand("SELECT * FROM MyData", con);
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            
            while (reader.Read())
            {
               if (reader[1].ToString() == "1")
               {
                  model[0] = reader[2].ToString();
                  model[1] = reader[3].ToString();
                  model[2] = reader[4].ToString();
                  model[3] = reader[5].ToString();
                  model[4] = reader[6].ToString();
                  model[5] = reader[7].ToString();
               }
               if (reader[1].ToString() == "2")
               {
                  model[6] = reader[2].ToString();
                  model[7] = reader[3].ToString();
                  model[8] = reader[4].ToString();
                  model[9] = reader[5].ToString();
                  model[10] = reader[6].ToString();
                  model[11] = reader[7].ToString();
               }
               if (reader[1].ToString() == "3")
               {
                  model[12] = reader[2].ToString();
                  model[13] = reader[3].ToString();
                  model[14] = reader[4].ToString();
                  model[15] = reader[5].ToString();
                  model[16] = reader[6].ToString();
                  model[17] = reader[7].ToString();
               }
            }
         }
         
         
more code follows...

No comments:

Post a Comment