Inserting data into SQL Server database
-
I need to develop a small application, from which i need to insert data into the SQL Server. I am using ADO.Net. pls give me some sample code if possible. or refer me to a suitable article. Thank U.
-
I need to develop a small application, from which i need to insert data into the SQL Server. I am using ADO.Net. pls give me some sample code if possible. or refer me to a suitable article. Thank U.
Hello dear friend Please notice below codes We have a table with 3 fields as : (field1:Int, field2:NVarChar, field3:Bit) At first of your form add these namespaces:
using System.Data; using System.Data.SqlClient;
Now add these sample lines to your Button_Click() event :string strInsert="INSERT INTO table (field1,field2,field3) VALIUES (@field1,@field2,@field3)"; if (SqlConnection1.state.toString() != "Open") SqlConnection1.Open(); sqlCommand cmd = new SqlCommand(strInsert,sqlConnection1); cmd.Parameters.Add(new SqlParameter("@field1",SqlDbType.Int,4,"field1")); cmd.Parameters["@field1"].Value = 1000; cmd.Parameters.Add(new SqlParameter("@field2",SqlDbType.NVarChar,10,"field2")); cmd.Parameters["@field2"].Value = "California"; cmd.Parameters.Add(new SqlParameter("@field3",SqlDbType.Bit,1,"field3")); cmd.Parameters["@field3"].Value = true; cmd.ExecuteNonQuery();
Don't remember to define SqlConnection if you have any problem mail me Bye Bye -- modified at 2:13 Monday 8th May, 2006 -
Hello dear friend Please notice below codes We have a table with 3 fields as : (field1:Int, field2:NVarChar, field3:Bit) At first of your form add these namespaces:
using System.Data; using System.Data.SqlClient;
Now add these sample lines to your Button_Click() event :string strInsert="INSERT INTO table (field1,field2,field3) VALIUES (@field1,@field2,@field3)"; if (SqlConnection1.state.toString() != "Open") SqlConnection1.Open(); sqlCommand cmd = new SqlCommand(strInsert,sqlConnection1); cmd.Parameters.Add(new SqlParameter("@field1",SqlDbType.Int,4,"field1")); cmd.Parameters["@field1"].Value = 1000; cmd.Parameters.Add(new SqlParameter("@field2",SqlDbType.NVarChar,10,"field2")); cmd.Parameters["@field2"].Value = "California"; cmd.Parameters.Add(new SqlParameter("@field3",SqlDbType.Bit,1,"field3")); cmd.Parameters["@field3"].Value = true; cmd.ExecuteNonQuery();
Don't remember to define SqlConnection if you have any problem mail me Bye Bye -- modified at 2:13 Monday 8th May, 2006Really i am very thankfull 2 U.
-
I need to develop a small application, from which i need to insert data into the SQL Server. I am using ADO.Net. pls give me some sample code if possible. or refer me to a suitable article. Thank U.
for vb.net and asp.net imports system.data.sqlclient dim con as new sqlconnection dim cmd as new sqlcommand con.connectionstring = "Provider= the provider u use;user id= ;password= ; data source= sql server name ;initial catalog= database name " cmd.connection = con con.open() cmd.commandtext = "insert into tablename values('" & tid.text & "')" cmd.executenonquery() con.close() if u have any problem with this revert back vil help u out regards kalyan