如何通过存储过程在表中插入数据? [英] how can i insert data in table via stored procedure?

查看:87
本文介绍了如何通过存储过程在表中插入数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Friends,





i希望使用store procedure将文本框值插入数据库。任何人都可以帮助我吗?









问候

迪帕克

Hello Friends,


i want to insert textbox value into database using store procedure.can any one help me??




Regards
Deepak

推荐答案

MySQL程序:

Procedure in MySQL:
CREATE PROCEDURE insertToTable
(IN param1 CHAR(20))
BEGIN
  INSERT INTO table(field1) VALUES(param1)
END





程序代码:



Code in program:

MySqlCommand cmd = new MySqlCommand("insertToTable", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@param1", TextBox1.Text);
cmd.ExecuteNonQuery();





参考: http://dev.mysql.com/doc/refman/5.5/en/connector-net -tutorials-intro.html#connector-net-tutorials-stored-procedures [ ^ ]



开头


使用MySQL数据库的ASP.NET应用程序 [< a href =http://www.codeproject.com/KB/database/mysqlinaspnet.aspxtarget =_ blanktitle =New Window> ^ ]

http://learneveryday.net/asp-net/mysql / insert-in-to-mysql-with-asp-net-part-1 / [ ^ ]


这是使用商店程序将数据插入数据库的虚拟程序



商店程序定义

------------- -------------------------------

this is dummy program of Insert data into database using store procedure

store procedure defination
--------------------------------------------
create procedure sp_Test
(
    @Id int, @Name varchar(50)
)
Insert into table1(Id, FullName) values(@Id,@Name)



----------------------- ---------------------





电话商店的C#代码程序和在数据库中插入数据。(执行按钮点击事件)

----------------------------- ---------


--------------------------------------------


C# code for call store procedure and insert data in database.(perform it a button click event)
--------------------------------------

try
  {
     sqlConnection = new SqlConnection(dbConnectionString);
     SqlCommand command = new SqlCommand("sp_Test", sqlConnection);
     command.CommandType = CommandType.StoredProcedure;
     command.Parameters.Add("@Id", SqlDbType.VarChar).Value = txtId.Text;
     command.Parameters.Add("@Name", SqlDbType.DateTime).Value = txtName.Text;
     sqlConnection.Open();
     return command.ExecuteNonQuery();
     sqlConnection.Close();
  }
catch (SqlException ex)
  {
     Console.WriteLine("SQL Error" + ex.Message.ToString());
     return 0;
  }



------------------------------ -------



@Id和@Name是数据库字段参数值,如果你的表有更多字段只是连续添加像


-------------------------------------

@Id and @Name is database field parameter value if your table has more field just serially add like

command.Parameters.Add("@Id", SqlDbType.VarChar).Value = txtId.Text;
command.Parameters.Add("@Name", SqlDbType.DateTime).Value = txtName.Text;
.
.
.
.





谢谢



thanks


这篇关于如何通过存储过程在表中插入数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆