Dear Friends,
Could you please help me to find a answer for this? Since i'm new to C # programming these are very fundermental things for you......
I want to use a SQL procedure instead of the INSERT query in the given below coding:
private void InsertRecord()
{
SqlConnection s1 = new SqlConnection(con1.SetConnection());
System.Data.SqlClient.SqlTransaction transaction;
s1.Open();
transaction = s1.BeginTransaction();
try
{
string query1;
string ScheType = SchedCode.Text;
string Scheduledesp = ScheDesp.Text;
// How can you call for a SQL procedure here to Enter data to the table in the SQL Server DB.
query1 = "INSERT INTO CM_SCHEDTYPE VALUES('" + ScheType + "','" + Scheduledesp + "')";
SqlCommand com1 = new SqlCommand(query1, s1, transaction);
com1.ExecuteNonQuery();
transaction.Commit();
messageBoxOk("New record has been added.");
//ClearInputControls();
}
catch (SqlException ex)
{
transaction.Rollback();
MessageBox.Show(ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
s1.Close();
}
}
Ryan AlfordPosted Apr 9, 2008, 9:48 AM
Dinusha GamagePosted Apr 8, 2008, 2:13 AM
Thanks you very much for your reply. I need more clarifications on this....
Here in this code "InsertCustomer", Is this the SQL Procedure name? You have to define this using the SQL server query analyser and the name should be "InsertCustomer" . Is it?
And here "@CustomerID" is the parameter name that you have defined in the sql procedure. Is it?
SqlCommand cmd = new SqlCommand("InsertCustomer", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@CustomerID", id));
cmd.ExecuteReader();
Mike GoldPosted Apr 8, 2008, 12:52 AM
SqlCommand cmd = new SqlCommand("InsertCustomer", connection); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@CustomerID", id));cmd.ExecuteReader();