Click here to Skip to main content
15,921,062 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
insert-update-delete code for sql server in asp.net class--How do I change this code for sql server

class 1
C#
public class NoeAutoE
   {
       public UInt64 NoeAutoId { get; set; }
       public String Nam { get; set; }
       public String Tozihat { get; set; }

   }

class 2
C#
public class NoeAutoEx
{
    public static void Insert(NoeAutoE nau)
    {
        String SQL = "INSERT INTO NoeAuto (NoeAutoId,Nam,Tozihat)VALUES(" + nau.NoeAutoId.ToString() + ",'"+nau.Nam+"','"+nau.Tozihat+"')";
        DataBase db = new DataBase();
        db.Execute(SQL);
    }

    public static void Update(NoeAutoE nau)
    {
        String SQL = "UPDATE NoeAuto SET Nam='" + nau.Nam + "',Tozihat='" + nau.Tozihat + "' WHERE NoeAutoId=" + nau.NoeAutoId.ToString();
        DataBase db = new DataBase();
        db.Execute(SQL);
    }

    public static void Delete(NoeAutoE nau)
    {
        String SQL = "DELETE FROM NoeAuto WHERE NoeAutoId=" + nau.NoeAutoId.ToString();
        DataBase db = new DataBase();
        db.Execute(SQL);
    }

    public static DataTable Fill()
    {
        String SQL = "SELECT * FROM NoeAuto Order By NoeAutoId DESC";
        DataBase db = new DataBase();
        DataTable dt = new DataTable();
        dt = db.ExecuteSelect(SQL);
        return dt;

    }
    public static UInt64 FindNextNoeAutoId()
    {
        String SQL = "SELECT MAX(NoeAutoId) FROM NoeAuto";
        DataBase db = new DataBase();
        DataTable dt = new DataTable();
        dt = db.ExecuteSelect(SQL);

        if (dt.Rows[0][0].ToString() == "")
        {
            return 1;
        }
        else
        {
            return Convert.ToUInt64(dt.Rows[0][0].ToString()) + 1;

        }
    }

    public static NoeAutoE Fill(UInt64 NoeAutoId)
    {
        String SQL = "SELECT * FROM NoeAuto WHERE NoeAutoId=" +NoeAutoId.ToString() ;
        DataBase db = new DataBase();
        DataTable dt = new DataTable();
        dt = db.ExecuteSelect(SQL);
        NoeAutoE nau = new NoeAutoE();
        nau.NoeAutoId = UInt64.Parse(dt.Rows[0]["NoeAutoId"].ToString());
        nau.Nam = dt.Rows[0]["Nam"].ToString();
        nau.Tozihat = dt.Rows[0]["Tozihat"].ToString();
        return nau;

    }

    public static void FilldrpNoeAuto(ref DropDownList drpNoeAuto)
    {
        String SQL = "SELECT * FROM NoeAuto";
        DataBase db = new DataBase();
        DataTable dt = new DataTable();
        dt = db.ExecuteSelect(SQL);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            drpNoeAuto.Items.Add(dt.Rows[i]["Nam"].ToString());
        }

    }
}

How do I change this code for sql server???
Posted
Updated 28-Apr-13 3:47am
v4

1 solution

Hi,

For Insert/Update/Delete you can use

C#
public bool Insert(string sQry)
   {
       try
       {
con = new SqlConnection(WebConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);
           con.Open();
           SqlCommand cmd = new SqlCommand(sQry, con);
           cmd.ExecuteNonQuery();
           return true;
       }
       catch (Exception ex)
       {
           return false;
       }
       finally
       {
           con.Close();
       }
   }



And for select query you can use below code

C#
public DataTable LoadDataTable(string sQry)
   {
       DataTable dt = new DataTable();
       try
       {
         con = new SqlConnection(WebConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);
           SqlDataAdapter sDap = new SqlDataAdapter(sQry, con);
           sDap.Fill(dt);
           return dt;
       }
       catch (Exception ex)
       {
           return null;
       }
   }


Above code is vary simple and just for start coding with asp.net & sql , but in professional work query fired using stored procedure...
 
Share this answer
 
Comments
twinsmm 30-Apr-13 7:52am    
Thank you :) but I need to code to use stored procedure in asp.net web app.
I dont know how change my code for using storedprcedure.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900