Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C“VS-必须声明标量变量”@“全球”;_C#_Sql Server_Visual Studio 2012 - Fatal编程技术网

C# C“VS-必须声明标量变量”@“全球”;

C# C“VS-必须声明标量变量”@“全球”;,c#,sql-server,visual-studio-2012,C#,Sql Server,Visual Studio 2012,1C#VS-必须声明标量变量“@Globals”您不能有一个。以你的名义。所以改变 @Globals.currentUserId 到 它不需要与C#变量的名称匹配 @Globals.currentUserId中的“.”产生了问题。这是一个保留字符。将其重命名为@UserId,如下所示 SqlConnection conn = new SqlConnection(Properties.Settings.Default.ElearningConnectionString2); s

1C#VS-必须声明标量变量“@Globals”

您不能有一个。以你的名义。所以改变

@Globals.currentUserId

  • 它不需要与C#变量的名称匹配
@Globals.currentUserId中的“.”产生了问题。这是一个保留字符。将其重命名为@UserId,如下所示

SqlConnection conn = new SqlConnection(Properties.Settings.Default.ElearningConnectionString2);
        string sc;
        SqlCommand cmd;
        sc = "INSERT INTO tblUserAnswers (UserId, QuizDate, score) values (@UserId, @QuizDate, @Score)";              
         cmd = new SqlCommand(sc, conn);
         cmd.Parameters.AddWithValue("@UserId", Globals.currentUserId);
         cmd.Parameters.AddWithValue("@QuizDate", DateTime.Now);
         cmd.Parameters.AddWithValue("@Score", score);
        conn.Open();
        int re = cmd.ExecuteNonQuery();

        conn.Close();
        if (re == 1)
        {
            MessageBox.Show("Record saved");
        }

你不可能有一个好朋友。以你的名义。因此,将“@Globals.currentUserId”更改为“@GlobalsCurrentUserId”—它不需要与C变量的名称匹配,也不需要在查询和参数名称中尝试转义它:
@“Globals.currentUserId\”
。现在它给我这个错误。“(”附近的语法不正确。必须声明标量变量“@GETDATE”。请将@GETDATE()更改为@QuizDate
SqlConnection conn = new SqlConnection(Properties.Settings.Default.ElearningConnectionString2);
        string sc;
        SqlCommand cmd;
        sc = "INSERT INTO tblUserAnswers (UserId, QuizDate, score) values (@UserId, @QuizDate, @Score)";              
         cmd = new SqlCommand(sc, conn);
         cmd.Parameters.AddWithValue("@UserId", Globals.currentUserId);
         cmd.Parameters.AddWithValue("@QuizDate", DateTime.Now);
         cmd.Parameters.AddWithValue("@Score", score);
        conn.Open();
        int re = cmd.ExecuteNonQuery();

        conn.Close();
        if (re == 1)
        {
            MessageBox.Show("Record saved");
        }