Monday, August 13, 2012

How to insert the data into database using string builder in asp.net

It may be given a little bit confusion to us when we go through this one.Because we would know how to insert the data into data base using sql command and one more way is to assign the sql query to string then execute the string using command.Here i would like to give an example for how to use the string builder to do for database insertion process.
 SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ConnectionString);
 SqlCommand cmd;
 System.Text.StringBuilder sql = new System.Text.StringBuilder("insert into Test_Db(Name,ACompany,Email,Phone,ADD,Country)values('");
    
 sql.Append(name + "','");
 sql.Append(company + "','");
 sql.Append(Email + "','"); 
 sql.Append(phone + "','");
 sql.Append(add + "','");
 sql.Append(Country + "')");   
 cn.Open();
 cmd = new SqlCommand(sql.ToString(),cn);
 cmd.ExecuteNonQuery();

1 comment:

jelly andrews said...

I am glad you shared this one. This is really interesting. Thanks a lot for sharing.

Bel