Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.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上_C#_Sql_Sql Server_Database_Select - Fatal编程技术网

C# 似乎找不到如何从我的表列中取出一个值并将其放在标签C上

C# 似乎找不到如何从我的表列中取出一个值并将其放在标签C上,c#,sql,sql-server,database,select,C#,Sql,Sql Server,Database,Select,我找不到如何从名为QuestionID的主键中获取整数值并将其显示在标签上 我认为将此代码放在计时器中可能是个好主意,因为我希望它在每次添加记录行时都会更新 我会非常感谢任何帮助,因为我是一个有点新手!谢谢大家! 这是我非常可怜的尝试,我放弃了,因为我不知道如何从数据库中获取值: private void timer1_Tick(object sender, EventArgs e) { string connectionString = ConfigurationManager

我找不到如何从名为QuestionID的主键中获取整数值并将其显示在标签上

我认为将此代码放在计时器中可能是个好主意,因为我希望它在每次添加记录行时都会更新

我会非常感谢任何帮助,因为我是一个有点新手!谢谢大家!

这是我非常可怜的尝试,我放弃了,因为我不知道如何从数据库中获取值:

private void timer1_Tick(object sender, EventArgs e)
{
        string connectionString = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;
        SqlConnection connect = new SqlConnection(connectionString);
        connect.Open();

        SqlCommand command6 = new SqlCommand("SELECT ([QuestionID]) FROM Questions", connect);

        QuestionNum.Text = 
}
我的其余代码如下所示:

private void button1_Click(object sender, EventArgs e)
{
    string connectionString = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;

    SqlConnection connect = new SqlConnection(connectionString);
    connect.Open();

    int checkedradiobutton = 0;

    if(radioButton1.Checked)
    {
            checkedradiobutton = 1;
    }
    else if(radioButton2.Checked)
    {
            checkedradiobutton = 2;
    }
    else if(radioButton3.Checked)
    {
            checkedradiobutton = 3;
    }

    string QuestionText = QuestionBox.Text;
    string AnswerText = teacheranswerbox.Text;

    SqlCommand command5 = new SqlCommand(@"INSERT INTO Questions ([Actual answer], [Question Space], [Question Type]) VALUES (@AnswerText, @QuestionText, @checkedradiobutton)", connect);
    command5.Parameters.AddWithValue("@AnswerText", AnswerText);
    command5.Parameters.AddWithValue("@QuestionText", QuestionText);
    command5.Parameters.AddWithValue("@checkedradiobutton", checkedradiobutton);

    command5.ExecuteNonQuery();
}

private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
        if (radioButton2.Checked)
        {
            teacheranswerbox.Text = "You cannot store an answer for a long answer essay question";
            teacheranswerbox.ReadOnly = true;
        }
        else
        {
            teacheranswerbox.ReadOnly = false;
            teacheranswerbox.Text = "Enter the correct answer here";


        }
    }
我试图从p.s.获取数据的表格。问题ID自动递增:

CREATE TABLE [dbo].[Questions] 
(
     [QuestionID]     INT           IDENTITY (1, 1) NOT NULL,
     [Actual answer]  NVARCHAR (50) NULL,
     [Question Space] NVARCHAR (50) NULL,
     [Question Type]  INT           NULL,

     PRIMARY KEY CLUSTERED ([QuestionID] ASC)
);
如果您的QuesitionID是一列,那么您可以使用如下批处理语句返回数据库引擎为其分配的值

string cmdText =@"INSERT INTO Questions 
                  ([Actual answer], [Question Space], [Question Type]) 
                  VALUES (@AnswerText, @QuestionText, @checkedradiobutton);
                  SELECT SCOPE_IDENTITY();"
SqlCommand command5 = new SqlCommand(cmdText, connect);
command5.Parameters.AddWithValue("@AnswerText", AnswerText);
command5.Parameters.AddWithValue("@QuestionText", QuestionText);
command5.Parameters.AddWithValue("@checkedradiobutton", checkedradiobutton);

int result = Convert.ToInt32(command5.ExecuteScalar());
QuestionNum.Text = result.ToString();

SQL Server支持在同一命令中使用多个语句。用分号把它们分开。在这段代码中,我在第一次插入之后添加了一个对的调用,该调用返回分配给连接相同范围内标识字段的最后一个值。应使用调用而不是ExecuteOnQuery检索此值。最后一部分很重要,因为ExecuteOnQuery只告诉您命令添加/修改了多少行,而ExecuteScalar返回已执行查询的第一行第一列的值,即SCOPE_IDENTITY的值。您的问题是不知道如何获取返回的数据,不是吗

    SqlCommand command6 = new SqlCommand("SELECT ([QuestionID]) FROM Questions", connect);

    String s = "";

    using (SqlDataReader reader = command6.ExecuteReader())
    {
        while (reader.Read())
        {
        for (int i = 0; i < reader.FieldCount; i++)
        {
            s += (reader.GetValue(i));
        }
        }
    }

    QuestionNum.Text = s;

我还没有测试代码,但我认为它应该可以工作…

您是否正在尝试获取插入查询所插入行的QuestionID值?是的,我希望QuestionID值显示在标签上感谢您的帮助,很抱歉,我花了一些时间回复,因为我花了一些时间才理解所有这些不同的更改。什么是身份字段?那只是我的ID栏吗?此外,当我尝试运行此代码时,它会在“int result=intcommand5.ExecuteScalar;”处停止并表示在计算A2_Official_Project.exe时发生了“System.InvalidCastException”类型的未处理异常。其他信息:指定的强制转换无效。@mot375已修复。当您像对QuestionID那样将IDENTITY属性指定给列时,该列将根据确定的步长值的初始值递增。见识别号1,1。Sql Server提供SCOPE_标识的确切目的是检索此自动值。非常感谢您的帮助,使用您提供的代码,标签将显示每个问题ID。您知道如何使其显示最近添加的ID吗?这种方法是错误的。如果有很多行呢?如何找到正确的问题ID?在整个表上循环查找最后添加的一个?如果有许多并发用户向该表添加新记录,该怎么办?找到最后分配的标识值的唯一正确方法是使用SCOPE\u IDENTITY。看见