how to store data from c# into sql server data base
-
i need to know that if many values are entered into a c# form, then i want to add all those in the dataBase.so what will be the code lines.plz reply someone
-
i need to know that if many values are entered into a c# form, then i want to add all those in the dataBase.so what will be the code lines.plz reply someone
Create a
SqlCommand
toINSERT
the data in the database.SqlCommand cmd = new SqlCommand();
cmd.Connection = aConnection;
cmd.CommandText = "INSERT TableName(Column1, Column2, Column3) "+
"VALUES (@Value1, @Value2, Value3";
cmd.Parameters.Add("@Value1", theFirstValue);
cmd.Parameters.Add("@Value2", theSecondValue);
cmd.Parameters.Add("@Value3", theThirdValue);
cmd.ExecuteNonQuery();You will have to replace
theFirstValue
,theSecondValue
andtheThirdValue
with appropriate values. Also, you will have to restructure the INSERT statement to match your database. I've also assumed that you have a connection (aConnection
) to the database.
Upcoming events: * Glasgow Geek Dinner (5th March) * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos
-
i need to know that if many values are entered into a c# form, then i want to add all those in the dataBase.so what will be the code lines.plz reply someone
hi First write StoredProcedure like for Insert Create StoredProc nameofStoredProc ( @firstname varchar(20), @lastname varchar(20)) as insert into tablename ( firstname, lastname ) values ( @fisrtname, @lastname ) then start writing ADO.net code SqlConnection con=new SqlConnection("userid=""; password="";DataSource=""); con.Open(); SqlCommand cmd=new SqlCommand("nameofStroredProc",con); cmd.CommandType=CommandType.StoredProcedure; cmd.Parameter.Add(new SqlParameter("@firstname",SqlDbType.varchar); cmd.Parameter.Add(new SqlParameter("@lastname",SqlDbType.varchar); cmd.parameter[0].value= txtFirstName.text;//textbox value cmd.Parameter[1].value=txtlastName.Text;//textbox value cmd.ExecuteNonQuery(); con.Close(); WINNING IS NOT OUR DREAM, IT'S A HABIT HAVE A NICE DAY
-
hi First write StoredProcedure like for Insert Create StoredProc nameofStoredProc ( @firstname varchar(20), @lastname varchar(20)) as insert into tablename ( firstname, lastname ) values ( @fisrtname, @lastname ) then start writing ADO.net code SqlConnection con=new SqlConnection("userid=""; password="";DataSource=""); con.Open(); SqlCommand cmd=new SqlCommand("nameofStroredProc",con); cmd.CommandType=CommandType.StoredProcedure; cmd.Parameter.Add(new SqlParameter("@firstname",SqlDbType.varchar); cmd.Parameter.Add(new SqlParameter("@lastname",SqlDbType.varchar); cmd.parameter[0].value= txtFirstName.text;//textbox value cmd.Parameter[1].value=txtlastName.Text;//textbox value cmd.ExecuteNonQuery(); con.Close(); WINNING IS NOT OUR DREAM, IT'S A HABIT HAVE A NICE DAY
dornala wrote:
SqlConnection con=new SqlConnection("userid=""; password="";DataSource="");
A SqlConnection needs both a server and database name. Also, It is better to use a trusted connection than supply a user name and password in plain text.
Upcoming events: * Glasgow Geek Dinner (5th March) * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos
-
dornala wrote:
SqlConnection con=new SqlConnection("userid=""; password="";DataSource="");
A SqlConnection needs both a server and database name. Also, It is better to use a trusted connection than supply a user name and password in plain text.
Upcoming events: * Glasgow Geek Dinner (5th March) * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos
hi Just Iam specifing a simple connection for an example. It is good practice to give entire connection object.
-
hi Just Iam specifing a simple connection for an example. It is good practice to give entire connection object.
dornala wrote:
It is good practice to give entire connection object.
I don't understand what you mean. What is a simple connection? The only way to connect to a database with ADO.NET is with a connection object.
Upcoming events: * Glasgow Geek Dinner (5th March) * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos