繁体   English   中英

在C#中更新SQL Server 2008的问题。 NET在运行时模式

[英]Problems in updating a SQL Server 2008 in C#. NET in runtime mode

我遇到了通过C#更新SQL Serve 2008中的行的问题。 NET应用程序。

在运行时,应用程序尝试更新数据库,但没有成功。 但是,没有异常,错误,没有。 检查SQL配置文件,发送了更新命令,但未提交。

如果我运行应用程序对其进行逐步调试(通过“ F11”),则该行将成功更新(!?!!?!?!!!!)

我已经复制了SQL更新命令,并在SQL Management Studio上运行了它,并且运行良好。

常规信息:-唯一的问题是在运行时模式下。 -使用的用户是具有所有已授予权限的“ sa”-我已经为其他表运行了“相同方法”(唯一更改的是表名),并且工作正常。

负责它的方法是:

    public void Save(FormaResult obj)
    {
        try
        {
            bool insert = GetById(obj.SLABID) == null;

            IList<string> colResult = GetColumns(TABLE);
            List<string> colList = colResult.Where(TableColumns.Contains).ToList();

            if (insert)
            {
                string col = string.Join(",", colList.Select(i => i).ToArray());
                string colParam = string.Join(", ", colList.Select(i => "@" + i).ToArray());
                QueryString = "INSERT INTO " + TABLE + " (" + col + ") VALUES(" + colParam + ");";
            }
            else
            {
                string colSet = string.Join(", ", colList.Select(i => i + " = @" + i).ToArray());
                QueryString = "UPDATE " + TABLE + " SET " + colSet + " WHERE SLABID = @Id1;";
            }

            DbCommand = Conn.CreateCommand();
            DbCommand.Connection = Conn;
            DbCommand.CommandText = QueryString;

            ListDbParameters = new List<DbParameter>
            {
                this.CriarParametro<DateTime>("GT_TIME", obj.GT_TIME),
                this.CriarParametro<long?>("SLABID", obj.SLABID),                    
                this.CriarParametro<short?>("STATUS", obj.STATUS)
            };

            if (!insert)
            {
                ListDbParameters.Add(this.CriarParametro<long>("Id1", obj.SLABID));
            }

            foreach (DbParameter param in ListDbParameters)
            {
                DbCommand.Parameters.Add(param);
            }

            Conn.Open();
            DbCommand.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            TrkCGManagedModuleService.Logger.Error(ex.Message);
            throw;
        }
        finally
        {
            Conn.Close();
        }
    }

我也使用了这种方法:

    public void OkEvt()
    {
        try
        {
            this.QueryString = "UPDATE " + TABLE + " SET STATUS = 1 " +
                               "FROM (SELECT TOP(1) * FROM " + TABLE + " WHERE STATUS=0 ORDER BY GT_TIME ASC) I " +
                               "WHERE " + TABLE + ".SLABID = I.SLABID AND " + TABLE + ".STATUS=0 AND " + TABLE + ".GT_TIME = I.GT_TIME;";

            this.DbCommand = this.Conn.CreateCommand();
            this.DbCommand.Connection = this.Conn;
            this.DbCommand.CommandText = this.QueryString;

            this.Conn.Open();
            this.DbCommand.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            TrkCGManagedModuleService.Logger.Error(ex.Message);
            throw;
        }
        finally
        {
            this.Conn.Close();
        }
    }

两种方法都有相同的目的,将“状态”列更新为“ 1”。

关于SQL注入,我会说和Soner Gonul一样。 我了解您要采取的措施,但根据我的经验,您离开时会打开一扇容易关闭的安全门。 只需花费更多时间为每个表编写CRUD语句。

这是我用于更新查询的代码示例,您可能会觉得有用。 如果要使用此方法,请记住创建一个拒绝读取器和拒绝写入器的用户。 然后运行命令以授予他们对所有存储过程的执行权限。

this.o_ConnectionString是在类构造函数中设置的私有变量,因为我将数据层放置在与Web应用程序不同的项目中。

public int UpdateThisTable(int TableUID, string SomeField)
{

        string StoredProcedure = "usp_SomeStoredProcedure";

        SqlConnection conn = new SqlConnection(enteryourconnectionstring);

        SqlCommand cmd = new SqlCommand(StoredProcedure, conn);
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.AddWithValue("@TableUID", TableUID);
        cmd.Parameters.AddWithValue("@SomeField", SomeField);

        conn.Open();
        int rowsaffected = cmd.ExecuteNonQuery();
        conn.Close();

        return rowsaffected;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM