我无法找到如何从主键(称为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. 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)
);
答案 0 :(得分:2)
作为您的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在同一命令中支持多个语句。用分号分隔它们。在这段代码中,我在第一次INSERT INTO之后添加了对SCOPE_IDENTITY的调用,该调用返回分配给连接相同范围内的IDENTITY字段的最后一个值。应使用ExecuteScalar调用而不是ExecuteNonQuery
来检索此值。最后一部分很重要,因为ExecuteNonQuery只告诉您命令添加/修改了多少行,而ExecuteScalar返回执行查询的第一行第一列的值(IE,SCOPE_IDENTITY的值)
答案 1 :(得分:0)
您的问题是,您不知道如何获取返回的数据不是吗?
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;
我没有测试过代码,但我认为它应该有用......