Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello every one,
please let me how can i make a common database for all of the classes in my application.

And how can i insert into table,update table,etc.

if any body have a sample code please provide me..

Thank you in advance
Posted
Updated 17-Jun-11 12:47pm
v4
Comments
Orcun Iyigun 17-Jun-11 18:47pm    
Please do not provide e-mail address in your threads.
theanil 17-Jun-11 18:57pm    
k...

i think this example help you :-

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.IO;
namespace abc
{
    class clsDeclerson
    {
        
        public static SqlConnection cn = new SqlConnection();
        
        public static void connectionOpen()
              {
                   cn.ConnectionString = "Data Source=" + server name+ ";Initial Catalog=SMS;User ID=" + server user Name+ ";Password=" + SQLPassword + "";

                   if (cn.State == ConnectionState.Closed) { cn.Open(); }
               }
        public static void insertUpdateDelete()
               {
                   SqlCommand comd = new SqlCommand("insert into abc(id,name) values(1,rr)",cn);
                
                   comd.ExecuteNonQuery();

                   SqlCommand comdU = new SqlCommand("update abc set=name=rr where id=1",cn);
                
                  comdU.ExecuteNonQuery();

                  SqlCommand comdD = new SqlCommand("delete abc where id=1",cn);
                
                  comdD.ExecuteNonQuery();


               }
            }
}
 
Share this answer
 
Simple example to start with,

Connecting To Sql Server 2008 Sample Database[^]

Hope it helps.
 
Share this answer
 
Its better to make an global class which consisting code for opening Gloabal Connection as well as DML operation

C#
public static SqlConnection GetConnection()
      {
          SqlConnection con = new SqlConnection("Your Connection String");
          con.Open();
          return con;
      }


or create an global function for Save,Update and Delete

C#
public static bool SaveRecord(string query)
        {
            int i = 0;
              cmd = new SqlCommand(query, GetConnection());
              try
              {
                  i = cmd.ExecuteNonQuery();
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex.Message.ToString());
              }
                if (i == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
        }



How to using SaveRecord(string query) Function

Just passed query to SaveFunction(string query) method as a parameter and hold value in a boolean.
Ex:-
bool res = SaveRecord("Your Command");
if(res)
{
//Saved
}
 
Share this answer
 
v2

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