![](/img/trans.png)
[英]How can I take the contents of a C# class and put into / take out of a string?
[英]Can't seem to find how to take a value out of my table column and put it on a label C#
我找不到如何從主鍵(稱為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";
}
}
我嘗試從中獲取數據的表(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)
);
將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返回已執行查詢的第一行第一列的值(即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;
我尚未測試代碼,但我認為它應該可以工作...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.