SqlCommand sqlcmd = new SqlCommand("Select * from customers"); //Using the northwhind//SqlConnection is class which will open the connection to Northwind Databasesqlcmd.Connection = SQLConnections.sqlCon;SqlDataAdapter sqda = new SqlDataAdapter(sqlcmd);DataTable dtTemp = new DataTable();SqlCommandBuilder sqlbuilder = new SqlCommandBuilder(sqda);SQLConnections.OpenConnection();sqda.Fill(dtTemp); //fill the values returned into temp data tableDataRow dr = dtTemp.NewRow(); //Creating a new rowdr[”CustomerID”] = “100"; //Add the custumer id //Requireddr[”CompanyName”] = “SolidSollutions”; //Adding the companyNamedr[”ContactName”] = “Danasegarane”;dr[”ContactTitle”] = “Programmer”;dr[”Address”] = “India”;dr[”City”] = “India”;dr[”Region”] = “India”;//I have left other columns fields because they are not the mandatory onedtTemp.Rows.Add(dr); //Adding the row to the datatablesqda.Update(dtTemp); //Update the related dataadapetersqda.Dispose(); //disposeSQLConnections.CloseConnection();
public class SQLConnections{public static SqlConnection sqlCon;public static string strErrorDescription = string.Empty;public static void OpenConnection(){string SQLServerName = string.Empty;string SQLDataBase = string.Empty;string SQLConnectionString = string.Empty;SQLServerName = ConfigurationManager.AppSettings[”SQLServerName”];SQLDataBase = ConfigurationManager.AppSettings[”SQLDataBase”];SQLConnectionString += “data source=” + SQLServerName + “;”;SQLConnectionString += “initial catalog=” + SQLDataBase + “;”;SQLConnectionString += “user id=sa;Password=test;”;SQLConnectionString += “persist security info=True “;sqlCon = new SqlConnection(SQLConnectionString);try{if (sqlCon.State == ConnectionState.Closed){sqlCon.Open();}}catch (Exception ex){strErrorDescription = ex.Message;}}//Change the SQLServerName and SQLDataBase to user requirements