Command.ExecuteNonQuery未生效 [英] Command.ExecuteNonQuery Not Take Effect

查看:112
本文介绍了Command.ExecuteNonQuery未生效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专家,



我正在使用Microsoft Visual Studio 2005和MSSQL Server 2005.



在我的网站中,我使用系统DSN进行ODBC连接。

在我的获取数据代码中,它成功运行。



但是,在我的数据库插入数据代码中,

我执行非查询,这在数据库中没有生效。



我的功能如下:



  public   int  Savedata(OdbcParameter [] p)
{
int i = 0 ;
con = new OdbcConnection(ConfigurationManager.ConnectionStrings [ connStr]的ToString());
cmd = new OdbcCommand();
cmd.Connection = con;
尝试
{
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = Save_Employee;
cmd.Parameters.Clear();
cmd.Parameters.AddRange(p);
i = cmd.ExecuteNonQuery();
return i;
}
catch (例外情况)
{
throw ex;
}
最后
{
con.Close();
cmd.Dispose();
con.Dispose();
}
}



我的连接字符串是:

< add name =   connStr connectionString =  < span class =code-string> DSN = TestODBC; Uid = myUserName; Pwd = MyPassword; /> 





请注意 -

1.我的所有代码都可以正常使用OdbcConnection的SqlConnection。

2.存储过程Save_Employee工作正常SqlConnection发布了OdbcConnection。

3.但是当我将所有代码转换为OdbcConnection时,ExecuteNonQuery成功但没有任何记录插入数据库。



任何人都可以帮助我...?

提前感谢您的任何帮助。

解决方案

你必须使用完整的ODBC调用语法。它也没有使用命名参数,所以你会在每个参数的位置使用一个问号

 cmd.CommandText =exec Save_Employee?,?,? ; 



如果您有3个参数。



进一步阅读:

http://msdn.microsoft.com/en-us/library /system.data.odbc.odbccommand.commandtype.aspx [ ^ ]


如何将需要更新的参数传递给存储过程。我认为你缺少很多 SqlParameters ,你也不需要调用 cmd.Parameters.Clear();



如果您的存储过程不期望来自外部世界的任何参数,那么我们可能需要查看该过程以找出问题。

Hi experts,

I am Using Microsoft Visual Studio 2005 and MSSQL Server 2005.

In my Website i am using ODBC Connection using "System DSN".
In my code of get Data, it is working successfully.

But, in my code of Insert Data to Database,
I am Execution Non Query which is not taking effect in database.

My Function is as under.

public int Savedata(OdbcParameter[] p)
    {
        int i = 0;
        con = new OdbcConnection(ConfigurationManager.ConnectionStrings["connStr"].ToString());
        cmd = new OdbcCommand();
        cmd.Connection = con;
        try
        {
            con.Open();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Save_Employee";
            cmd.Parameters.Clear();
            cmd.Parameters.AddRange(p);
            i = cmd.ExecuteNonQuery();
            return i;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            cmd.Dispose();
            con.Dispose();
        }
    }


My Connection string is :

<add name="connStr" connectionString="DSN=TestODBC;Uid=myUserName;Pwd=MyPassword;"/>



Please note that -
1. my all code is working fine with SqlConnection insted of OdbcConnection.
2. Stored Procedure "Save_Employee" is working fine with SqlConnection insted of OdbcConnection.
3. but when i convert all code to OdbcConnection, the ExecuteNonQuery is success but no any record inserted in database.

Any one can help me...?
Thanks in advance your any kind of help.

解决方案

You must use the full ODBC call syntax. It also doesn''t use named parameters, so you would use a question mark in each param''s place

cmd.CommandText = "exec Save_Employee ?, ?, ?";


if you had 3 parameters.

For further reading:
http://msdn.microsoft.com/en-us/library/system.data.odbc.odbccommand.commandtype.aspx[^]


How are you passing the parameters that needs to be updated to the stored procedure. I think you are missing a lot of SqlParameters and you also don''t need to call the cmd.Parameters.Clear();

If your stored procedure is not expecting any parameter from the outside world then perhaps we need to look into the procedure to figure out the problem.


这篇关于Command.ExecuteNonQuery未生效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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