本文介绍了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

推荐答案


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();
    }
}


command.ExecuteNonQuery();



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



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


这篇关于C#中的执行程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 06:52