我正在尝试构建测验应用程序。我创建了两个表格:如何检查一个表的ID是否等于另一个表的ID,如果相等,则将新值赋给asp.net表中的另一列
CREATE TABLE [dbo].[Answers] (
[AID] INT NOT NULL,
[QID] INT NOT NULL,
[Answer] NVARCHAR (200) NOT NULL,
[CorrectAnswer] NVARCHAR (200) NULL,
PRIMARY KEY CLUSTERED ([AID] ASC),
CONSTRAINT [FK_Answers_Question] FOREIGN KEY ([QID]) REFERENCES [dbo].
[Question] ([QID])
);
CREATE TABLE [dbo].[Question] (
[QID] INT IDENTITY (1, 1) NOT NULL,
[Question] NVARCHAR (200) NOT NULL,
PRIMARY KEY CLUSTERED ([QID] ASC)
);
这里,QID是自动生成和递增的。 答案表包含作为主键的AID和作为外键的QID。 现在我的问题是我应该自动生成并增加Answers.QID还是应该单独设置?而且我想Answers.AID增加到4(多选题4个选项)。 事情是这样的:
Answers Table
AID | QID | Answer
1 | 1 | Scripting Language
2 | 1 | Programming Language
3 | 1 | MarkUp Language
4 | 1 | Network Protocol
Question Table
QID | Question
1 | What is HTML?
所以每当我尝试插入的回答表的答案,应该检查是否Question.QID = Answers.QID。如果为true,则从1到4分配Answer.AID并输入答案。
所有这些都由管理员管理,所以我创建了一个Web表单AddQuestion.aspx,当管理员单击添加按钮时可以添加问题,并将其重定向到管理员可以保存并上载答案的AddAnswer.aspx。
AddQuestion.aspx
public partial class AddQuestion : System.Web.UI.Page
{
SqlCommand command;
SqlConnection connection;
protected void ButtonAdd_Click(object sender, EventArgs e)
{
connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TheQuizConnectionString"].ConnectionString);
connection.Open();
string addQuestion = "insert into Question(Question) values(@Ques)";
command = new SqlCommand(addQuestion, connection);
//command.Parameters.AddWithValue("@Qid", TextBoxQid.Text);
command.Parameters.AddWithValue("@Ques", TextBoxQuestion.Text);
command.ExecuteNonQuery();
Response.Redirect("AddAnswer.aspx");
connection.Close();
}
}
AddAnswer.aspx
public partial class AddAnswer : System.Web.UI.Page
{
SqlCommand command;
SqlConnection connection;
protected void ButtonAdd_Click(object sender, EventArgs e)
{
Response.Redirect("AddQuestion.aspx");
}
public void CheckQid()
{
connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TheQuizConnectionString"].ConnectionString);
connection.Open();
string checkQid = "select count(QID) from Question";
using (command = new SqlCommand(checkQid, connection))
}
protected void ButtonSave_Click(object sender, EventArgs e)
{
connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TheQuizConnectionString"].ConnectionString);
connection.Open();
string addAnswer = "insert into Answers(Answer) values(@Answer)";
command = new SqlCommand(addAnswer, connection);
//command.Parameters.AddWithValue("@Aid", TextBoxAid.Text);
//command.Parameters.AddWithValue("@Qid", TextBoxQid.Text);
command.Parameters.AddWithValue("@Answer", TextBoxAnswer.Text);
command.ExecuteNonQuery();
Response.Redirect("AddAnswer.aspx");
}
}
我是新来asp.net和视觉工作室。我试图寻找答案,但找不到任何东西。我很感激,如果有人能帮助我这个。
谢谢。
运行'command.ExecuteNonQuery'时会发生什么错误?你也在哪里执行这个方法实际的SQL查询'公共无效CheckQid()' – MethodMan
我敢肯定它没有QID参数 –
@MethodMan我不写怎么写sql查询,我只是试过这个:Update解决方案设置QID =(从问题中选择QID where Question.QID = Answer.QID)但它没有奏效。而且我也没有那么多的关于sql的知识。 –