EDN Admin
Well-known member
I was already able to get the value of scope_identity using a stored procedure, now to further enhance my knowledge I also want to learn on how to use it without using a stored procedure.
Here is my code using a stored procedure:
<pre class="prettyprint string connect = "Data Source=RANDEL-PC;Initial Catalog=Randel;Integrated Security=True";
SqlConnection connection = new SqlConnection(connect);
connection.Open();
string insert = "NewRow";
SqlCommand command = new SqlCommand(insert, connection);
#region Method2
//method2
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "Fname";
parameter.Value = textBox1.Text;
command.Parameters.Add(parameter);
SqlParameter p2 = new SqlParameter();
p2.ParameterName = "Lname";
p2.Value = textBox2.Text;
command.Parameters.Add(p2);
SqlParameter p3 = new SqlParameter();
p3.ParameterName = "Age";
p3.Value = textBox3.Text;
command.Parameters.Add(p3);
command.CommandType = CommandType.StoredProcedure;
SqlParameter p4 = new SqlParameter();
p4.ParameterName = "Id";
p4.Direction = ParameterDirection.Output;
p4.SqlDbType = SqlDbType.Int;
command.Parameters.Add(p4);
command.ExecuteNonQuery();
button1.Text = p4.Value.ToString();
command.Dispose();
connection.Close();
connection.Dispose();
#endregion[/code]
<br/>
Heres my current code without using a stored procedure, honestly Im kinda stuck and I really want to learn how to do it.
<pre class="prettyprint string connect = "Data Source=RANDEL-PC;Initial Catalog=Randel;Integrated Security=True";
SqlConnection connection = new SqlConnection(connect);
connection.Open();
//string insert = "NewRow " + textBox1.Text + ", " + textBox2.Text + ", " + textBox3.Text + "";
string insert = "Insert into SamplePerson (Fname,Lname,Age) Values("+textBox1.Text+","+textBox2.Text+","+textBox3.Text+")";
SqlCommand command = new SqlCommand(insert, connection);
command.ExecuteNonQuery();
string get = "Select Scope_Identity()";
command.CommandText = get;[/code]
<br/>
Sir/Maam your answers would be of great help. Thank you++.
<
Randel Ramirez
<br/>
View the full article
Here is my code using a stored procedure:
<pre class="prettyprint string connect = "Data Source=RANDEL-PC;Initial Catalog=Randel;Integrated Security=True";
SqlConnection connection = new SqlConnection(connect);
connection.Open();
string insert = "NewRow";
SqlCommand command = new SqlCommand(insert, connection);
#region Method2
//method2
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "Fname";
parameter.Value = textBox1.Text;
command.Parameters.Add(parameter);
SqlParameter p2 = new SqlParameter();
p2.ParameterName = "Lname";
p2.Value = textBox2.Text;
command.Parameters.Add(p2);
SqlParameter p3 = new SqlParameter();
p3.ParameterName = "Age";
p3.Value = textBox3.Text;
command.Parameters.Add(p3);
command.CommandType = CommandType.StoredProcedure;
SqlParameter p4 = new SqlParameter();
p4.ParameterName = "Id";
p4.Direction = ParameterDirection.Output;
p4.SqlDbType = SqlDbType.Int;
command.Parameters.Add(p4);
command.ExecuteNonQuery();
button1.Text = p4.Value.ToString();
command.Dispose();
connection.Close();
connection.Dispose();
#endregion[/code]
<br/>
Heres my current code without using a stored procedure, honestly Im kinda stuck and I really want to learn how to do it.
<pre class="prettyprint string connect = "Data Source=RANDEL-PC;Initial Catalog=Randel;Integrated Security=True";
SqlConnection connection = new SqlConnection(connect);
connection.Open();
//string insert = "NewRow " + textBox1.Text + ", " + textBox2.Text + ", " + textBox3.Text + "";
string insert = "Insert into SamplePerson (Fname,Lname,Age) Values("+textBox1.Text+","+textBox2.Text+","+textBox3.Text+")";
SqlCommand command = new SqlCommand(insert, connection);
command.ExecuteNonQuery();
string get = "Select Scope_Identity()";
command.CommandText = get;[/code]
<br/>
Sir/Maam your answers would be of great help. Thank you++.
<
Randel Ramirez
<br/>
View the full article