在C#中更新SQL Server 2008时出现问题.NET处于运行时模式

本文关键字:NET 问题 模式 运行时 更新 SQL 2008时 Server | 更新日期: 2023-09-27 18:20:27

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

在运行时,应用程序尝试更新数据库,但没有成功。然而,没有例外,没有错误,什么都没有。检查SQL配置文件时,更新命令已发送,但未提交。

如果我一步一步地运行应用程序调试它(通过"F11"),行将成功更新(!?!?!!?!)

我已经复制了SQL更新命令,并在SQL Management Studio上运行了它,工作也很好。

一般信息:-唯一的问题是运行时模式。-使用的用户是"sa",具有所有授予的权限-我已经为其他表运行了SAME方法(唯一改变的是表名),它运行得很好。

负责它的方法是:

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

两种方法的目标相同,请将列"STATUS"更新为"1"。

在C#中更新SQL Server 2008时出现问题.NET处于运行时模式

关于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;
}