C#中的执行程序

编程入门 行业动态 更新时间:2024-10-27 05:29:06
本文介绍了C#中的执行程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我想在C#中执行一个sql存储过程.该过程只是将数据插入表中,因此它没有参数.我知道该怎么做,不确定连接打开后会怎样. :(

Hi everyone, I want to execute a sql stored procedure in C#. The procedure just inserts data into a table, therefore it has no parameters. I know how to do this,no sure what goes after the connection is open. :(

string connectionString = "connect string...etc"; string sqlstring = "execute myprocedure"; OleDbConnection connection = new OleDbConnection(connectionString); OleDbCommand command = new OleDbCommand(sqlstring,connection); command.CommandType = CommandType.StoredProcedure; try { connection.open(); ?????????????????????? connection.close(); } catch (exception)

请帮忙. nicole

Please help. nicole

推荐答案

请查看以下线程: 在ADO.NET中调用存储过程 [ ^ ] 使用该C#代码运行任何存储过程 [ ^ ] 在C#中调用存储过程 [ ^ ] Please have a look on following threads: Calling Stored procedures in ADO.NET[^] Run any stored procedure using that C# code[^] call stored procedure in C# [^]

1. 错了! 1. This is wrong! sqlstring = "execute myprocedure";

如果您打算使用 CommandType.StoredProcedure ,则应为:

If you plan to use CommandType.StoredProcedure it should be:

sqlstring = "myprocedure";

2. 试试这个:

2. Try this:

using(OleDbConnection connection = new OleDbConnection(connectionString)) { OleDbCommand command = new OleDbCommand("myprocedure",connection); command.CommandType = CommandType.StoredProcedure; try { connection.open(); command.ExecuteNonQuery(); } catch (OledbExcpetion olex) { Debug.WriteLine(olex.ToString()); } catch (Exception ex) { Debug.WriteLine(ex.ToString()); } finally { //if you are using try/catch block //this is better place for default connection closing connection.Close(); } }

只需执行命令: Just execute the Command: command.ExecuteNonQuery();

如果您的过程不需要参数,它将执行过程.

It will Execute Procedure if your procedure does not require Parameters.

更多推荐

C#中的执行程序

本文发布于:2023-11-13 09:30:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584012.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!