【问题标题】:Session Variable value are not storing into MS Sql Server Database会话变量值未存储到 MS Sql Server 数据库中
【发布时间】:2017-06-18 08:21:55
【问题描述】:

我正在使用两个会话变量,我将会话变量分配给另一个变量并显示它从会话变量中正确获取值,但是当我尝试插入 MS Sql Server 数据库时,该值没有插入数据库...有什么想法吗?

以下是我的代码:

Session["selected"] = "apple";
Session["current"] = 1;

string mycategory = Session["selected"].ToString();
int myId = Convert.ToInt32(Session["current"]);

con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO [Marking] VALUES (@photoid, @photocategoryjudge, @totalmarks)", con);
cmd.Parameters.Add("@photoid", SqlDbType.Int).Value = myId;
cmd.Parameters.Add("@photocategoryjudge", SqlDbType.VarChar).Value = mycategory;
cmd.Parameters.Add("@totalmarks", SqlDbType.Int).Value = totalMarks;
cmd.ExecuteNonQuery();
con.Close();

【问题讨论】:

  • 是否设置了totalMarks?这是Marking 表中的必填字段吗? Marking 表中是否还有其他列未使用此 INSERT 语句设置?明确说明您要插入哪些列可能会更好:INSERT INTO [Marking] (PhotoID, PhotoCategory, TotalMarks) VALUES (...
  • 是的,totalMarks 也已设置,我尝试 INSERT INTO [Marking] (PhotoID, PhotoCategory, TotalMarks) VALUES (...它仍然没有存储到数据库中...
  • 尝试调试以查看插入前存在哪些值
  • 如果您尝试直接在服务器(Management Studio)上运行INSERT,它可以工作吗?我们需要尝试确定这是数据库问题(即 SQL Server 不允许您插入此行的原因)还是代码问题。
  • 等等...我认为 INSERT 语句不起作用.. 我不知道为什么,因为以前它在起作用

标签: c# asp.net sql-server database razor


【解决方案1】:

我没有答案,但我有一些简单的步骤可以帮助您进行调试,并且这些步骤的基础是良好的编码标准,并且可以用于未来项目的一些变体中,强>

首先要做的是实现异常处理,这将通过try...catch...finally 块来完成。您将尝试大部分实际 SQL 命令的代码块,如果出现错误,它会将您扔到 Catch 块中。将变量分配给异常将允许您悬停以查看详细信息。 catch 可以针对不同的条件进行堆叠,Exception 是它的最后一个,它将捕获所有类型的异常。这有点像case 声明,因为只有一个 catch 系列会触发。该块的最后一部分是 Finally 语句,它将在预期的命令完成或发生异常处理后运行。

您没有使用的是 ExecuteNonQuery() 方法实际上有一个返回值 (int32),它表示 Sql 语句的 Rows Affected 值。对于一个简单的插入语句,这应该是 1。如果这是一个更新命令,这可能是 0 或更高,这取决于有多少行或任何条件。无论哪种方式,它都将始终等于或大于 0。我在这里的实现方式,如果编码中有错误,它将分配一个负值,并为每种类型的异常分配不同的值。

最后一件事是在带有断点的调试模式下运行您的代码。当你点击断点时,看看你的哪些变量有什么值。这将有助于了解发生了什么错误(如果有)。您可以检查您的 Sql 语句,如果这是错误源,请尝试直接在 SSMS 中运行。

祝你好运

Session["selected"] = "apple";
Session["current"] = 1;

string mycategory = Session["selected"].ToString();
int myId = Convert.ToInt32(Session["current"]);

int ra; //                                 ra = Rows Affected

try {
    con.Open();
    SqlCommand cmd = new SqlCommand("INSERT INTO [Marking] VALUES (@photoid, @photocategoryjudge, @totalmarks)", con);
    cmd.Parameters.Add("@photoid", SqlDbType.Int).Value = myId;
    cmd.Parameters.Add("@photocategoryjudge", SqlDbType.VarChar).Value = mycategory;
    cmd.Parameters.Add("@totalmarks", SqlDbType.Int).Value = totalMarks;
    ra = cmd.ExecuteNonQuery();
}
catch (SqlException sx) {
    ra = -2; //                             breakpoint here
    // If you stop here, your SQL has an error. Hover on sx for detail
    // Error handling routine
catch (Exception ex) {
    ra = -1; //                             breakpoint here
    // non-sql error block. Hover on ex for more info
    // Error handling routine
}
finally {

    con.Close();
}

int Results = ra; //                        breakpoint here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    • 2015-10-09
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多