本文介绍了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#中的执行程序
发布评论