[英]Can't seem to find how to take a value out of my table column and put it on a label C#
I can't find how to take the integer value from my primary key (called QuestionID
) and display it on a label. 我找不到如何从主键(称为QuestionID
)中获取整数值并将其显示在标签上的方法。
I thought it might be a good idea to have this code in a timer because I want it to update every time a record (row) is added. 我认为在计时器中包含此代码可能是一个好主意,因为我希望它在每次添加记录(行)时都进行更新。
I would very much appreciate any help in any way as I'm a bit of a newbie! 我非常感谢任何新手,以任何方式提供的任何帮助! Thank you! 谢谢!
Here is my very pitiful attempt to doing it which I kind of gave up on as I don't know how to take the value from the database: 这是我所做的非常可怜的尝试,由于我不知道如何从数据库中获取价值,我有点放弃了:
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 =
}
The rest of my code is here: 我的其余代码在这里:
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";
}
}
The table I am trying to take the data from (ps the QuestionID
automatically increments): 我尝试从中获取数据的表(ps QuestionID
自动递增):
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)
);
Being your QuesitionID
an IDENTITY column then you could get back the value assigned to it by your database engine using a batch statement like this 将QuesitionID
作为IDENTITY列,然后您可以使用这样的批处理语句取回数据库引擎分配给它的值
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 supports multiple statement in the same command. SQL Server在同一命令中支持多个语句。 Just separe them with a semicolon. 只需用分号分隔它们即可。 In this code I have added, just after the first INSERT INTO, a call to SCOPE_IDENTITY that returns the last value assigned to an IDENTITY field in the same scope of your connection. 在此代码中,我在第一个INSERT INTO之后添加了对SCOPE_IDENTITY的调用,该调用返回在连接的相同范围内分配给IDENTITY字段的最后一个值。 This value should be retrieved using an ExecuteScalar call instead of an ExecuteNonQuery
. 应该使用ExecuteScalar调用而不是ExecuteNonQuery
检索此值。 The last part is important because ExecuteNonQuery tells you only how many rows have been added/modified by the command, while ExecuteScalar returns the value of the first row first column of the executed query (IE, the value of SCOPE_IDENTITY) 最后一部分很重要,因为ExecuteNonQuery只告诉您命令已添加/修改了多少行,而ExecuteScalar返回已执行查询的第一行第一列的值(即SCOPE_IDENTITY的值)
Your Problem is that you don't know how to get the Data that is returned isn't it? 您的问题是您不知道如何获取返回的数据,不是吗?
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;
I have not tested the code but I think it should work... 我尚未测试代码,但我认为它应该可以工作...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.