Sql stored procedure,Parameters

Posted by Raja_89 under Sql Server on 6/19/2015 | Points: 10 | Views : 2124 | Status : [Member] | Replies : 3
Hi
How can we write a stored procedure to use like this

con.open();
Sqlcommand sqlcmd= new sqlcommand("insert into aa values(c1,c2)",con);
sqlcmd.executeNonquery()
con.close();

Passing the query from Front-end [C# win form OR C# web form] to this stored procedure for various tables in my db.




Regards




Responses

Posted by: Bandi on: 6/19/2015 [Member] [MVP] Platinum | Points: 25

Up
0
Down
con.open(); 
Sqlcommand sqlcmd= new sqlcommand("insert into aa(c1,c2) values(@c1,@c2) ",con);
sqlcmd.Parameters.AddWithValue("@c1", c1TextBox.Text);
sqlcmd.Parameters.AddWithValue("@c2", c2TextBox.Text);

sqlcmd.executeNonquery()
con.close();


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Raja_89, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 6/19/2015 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
try this

SqlConnection con = new SqlConnection("Data Source=PcNAME;Initial Catalog=Test;Integrated Security=true;");
con.open();
Sqlcommand sqlcmd= new sqlcommand("insert into aa(c1,c2) values('"+ TextBox1.Text +"','"+ TextBox2.Text +"') ",con);
sqlcmd.executeNonquery()
con.close();

Mark as Answer if its helpful to you

Raja_89, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sheonarayan on: 6/21/2015 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Hi Raja,

Here are two articles from DotNetFunda.com that will help you get your solution

http://www.dotnetfunda.com/articles/show/375/insert-data-using-parameters-through-stored-procedure
http://www.dotnetfunda.com/articles/show/2225/insert-update-and-delete-in-data-through-one-windows-form

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Raja_89, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response