【问题标题】:sql stored procedure in asp.net not runningasp.net中的sql存储过程未运行
【发布时间】:2012-12-04 18:53:11
【问题描述】:

关于我正在做的事情的一点背景。 我有一个带有点击调用的按钮,可以将我带到这段代码。

  static public DataSet shareFiles(TransitoryRegObj argTransRegObj)
{
    string sqlString = "do_share_files"; // it's a stored procedure
    SqlConnection cnn = new SqlConnection(masterConn);
    SqlCommand comm = new SqlCommand(sqlString, cnn);
    DataSet ds = new DataSet();

    try
    {
        cnn.Open();
        SqlCommand Comm = new SqlCommand(sqlString, cnn);
        Comm.CommandType = CommandType.StoredProcedure;


        comm.Dispose();
        cnn.Close();

        return ds;
    }
    catch (Exception ex)
    {
        // log here should anything go wrong with anything
        //  lblmessage.Text = "Error: " + ex.Message;



        if (comm != null)
            comm.Dispose();

        if (cnn != null)
            cnn.Close();

        DataTable dt = new DataTable("ExceptionTable");
        dt.Columns.Add("ExceptionMessage");
        dt.Rows.Add(ex.Message);
        ds = new DataSet();
        ds.Tables.Add(dt);
        return ds;
    }
}

代码运行良好,但没有写入数据库。这是 do_share_files 存储过程。

  ALTER PROCEDURE [dbo].[do_share_files] 
   --@device_id bigint, @user_id bigint, @file_name varchar(50),@full_up_path varchar(50), @upLength varchar(30)
    --,@mime_type varchar(20), @filedate varchar(30)

    AS
    BEGIN
        insert into [user_files] (device_id, user_id, original_name, original_path, up_path, content_type, up_dt)
        values (17, 30, 'test.pg', 'test.pg', 'test.pg','test.pg', '2012-11-15 03:58:06.043')


    END

我现在有静态值,因为我只是想让它运行到存储过程。 我是 asp.net 的新手,不知道我做错了什么。任何帮助,将不胜感激。

谢谢!

【问题讨论】:

标签: asp.net sql stored-procedures


【解决方案1】:

你可以从这个开始:

static public DataSet shareFiles(TransitoryRegObj argTransRegObj)
{
    string sqlString = "do_share_files"; // it's a stored procedure
    DataSet ds = new DataSet();

    try
    {
        using (var cnn = new SqlConnection(masterConn))
        { 
            SqlCommand comm = new SqlCommand(sqlString, cnn);
            comm.CommandType = CommandType.StoredProcedure;
            cnn.Open();
            comm.ExecuteNonQuery ();

总结一下:

  1. Commcomm 是不同的命令;
  2. 要运行 proc,您需要调用 ExecuteNonQuery 或其他 Execute 方法。

【讨论】:

  • 谢谢 Serge,我已经修好了
  • 感谢 Serge,我已修复它,但它仍然无法正常工作。当我调试它时;它到达 comm.ExecuteNonQuery();然后跳转到 Catch(Exeption ex)... 还有其他提示吗?
  • @FelipeMartins 请发布异常详情。
  • 我看了看就明白了。这只是数据类型的错误,试图将字符串放入日期。非常感谢兄弟!!
【解决方案2】:

您的代码几乎没有错误 1.我不明白你为什么要使用这行两次

SqlCommand comm = new SqlCommand(sqlString, cnn);

2。您没有执行主要问题的程序

static public DataSet shareFiles(TransitoryRegObj argTransRegObj)
{
try
{
string sqlString = "do_share_files"; // it's a stored procedure
SqlConnection cnn = new SqlConnection(masterConn);
SqlCommand comm = new SqlCommand(sqlString, cnn);
DataSet ds = new DataSet();  
    cnn.Open();
    Comm.CommandType = CommandType.StoredProcedure;
    comm.ExecuteNonQuery();
    comm.Dispose();
    cnn.Close();

    return ds;
}
catch (Exception ex)
{
   //something here
}

}

【讨论】:

  • 嘿赛义德,感谢您的更正。我修复了你告诉我的内容......但是当我调试它时;它到达 comm.ExecuteNonQuery();然后跳转到 Catch(Exeption ex)... 还有其他提示吗?
  • @FelipeMartins 请发布异常详情。
猜你喜欢
  • 1970-01-01
  • 2011-04-28
  • 1970-01-01
  • 2018-03-24
  • 1970-01-01
  • 2019-06-25
  • 2014-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多