如何从数据库重新加载数据 [英] How do reload data from database

查看:132
本文介绍了如何从数据库重新加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个包含学生记录和试卷的网络应用程序。我想从数据库重新加载数据,如果计算机在学生正在进行测试时突然关闭(可能是由于光线问题或笔记本电脑电池问题)。当计算机再次打开时,应再次显示所有标记的答案。我无法理解如何做到这一点。以下是我的代码。

任何人都可以告诉我该怎么做。



我尝试了什么:



使用System;

使用System.Collections;

使用System.Configuration;

使用System.Data;

使用System.Linq;

使用System.Web;

使用System.Web.Security;

使用System.Web.UI;

使用System.Web.UI.HtmlControls;

使用System.Web.UI.WebControls;

使用System.Web.UI.WebControls.WebParts;

使用System.Xml.Linq;

使用System.Data.Sql;

使用System.Data.SqlClient;



public partial class _Default:System.Web.UI.Page

{



SqlConnection Con;

SqlCommand Cmd;

SqlDataAdapter da;

SqlDataReader dr;

DataSet ds;

DataTable dt = new DataTable();

int recordcount = 0;

int i = 0;

// string strConnString = ConfigurationManager.ConnectionStrings [ConnectionString]。ConnectionString;

protected void Page_Load(object sender,EventArgs e)

{< br $>


Con = new SqlConnection(Data Source = DESKTOP-Q69PRF4; Initial Catalog = new; Integrated Security = True);

Cmd = new SqlCommand(SELECT * FROM student where category ='+ Session [test] +',Con);

da = new SqlDataAdapter(Cmd);

ds = new DataSet(student);



Con.Open();

this.da.Fill(ds, 学生);

Cmd =新的SqlCommand(select * from student where Question ='+ TextBox1.Text +',Con);

dr = Cmd.ExecuteReader();

while(dr.Read())

{

Label7.Text = dr [Q_ID]。 ToString();

lblmarks.Text = dr [TotalMarks]。ToString();



}





recordcount = ds.Tables [0] .Rows.Count;

Response.Write(记录计数:+记录计数);





if(recordcount> i)

{

TextBox1.Text = ds.Tables [0] .Rows [i] [Question]。ToString();

Label2.Text = ds.Tables [0] .Rows [i] [Choice1]。ToString();

Label3.Text = ds.Tables [0] .Rows [i] [Choice2]。ToString();

Label4.Text = ds.Tables [0] .Rows [i] [Choice3]。ToString();

Label5.Text = ds.Tables [0] .Rows [i] [Choice4]。ToString();

Label6.Text = ds.Tables [0] .Rows [i] [ Choice5]。ToString();

}

其他

{

Response.Write(那里没有此类别中的记录);

}

if(i == 0)

{

RadioButton1.Visible = true;

RadioButton2.Visible = true;

RadioButton3.Visible = true;

RadioButton4.Visible = true;

RadioButton5.Visible = true;

CheckBox1.Visible = false;

CheckBox2.Visible = false;

CheckBox3.Visible = false;

CheckBox4.Visible = false;

CheckBox5.Visible = false;



}

dr.Read();





Con.Close();



}





protected void Button2_Click(object sender,EventArgs e)

{



//Con.Open();

// Cmd = new SqlCommand(select * from student where Question ='+ TextBox1.Text +',Con);

// dr = Cmd.ExecuteReader();

// while(dr.Read())

// {

// Label7.Text = dr [Q_ID]。ToString();

//}

//Con.Close();



TextBox2.Text =(int.Parse(TextBox2.Text)+ 1).ToString();



i = Convert.ToInt32(TextBox2.Text);





TextBox1.Text = ds.Tables [0] .Rows [i] [Question]。ToString();

Label2.Text = ds.Tables [0] .Rows [i] [Choice1]。 ToString();

Label3.Text = ds.Tables [0] .Rows [i] [Choice2]。ToString();

Label4.Text = ds .Tables [0] .Rows [i] [Choice3]。ToString();

Label5.Text = ds.Tables [0] .Rows [i] [Choice4]。ToString ();

Label6.Text = ds.Tables [0] .Rows [i] [Choice5]。ToString();





if(i == 1)

{

RadioButton1.Visible = false;

RadioButton2。 Visible = false;

RadioButton3.Visible = false;

RadioButton4.Visible = false;

RadioButton5.Visible = false;

CheckBox1.Visible = true;

CheckBox2.Visible = true;

CheckBox3.Visible = true;

CheckBox4.Visible = true;

CheckBox5.Visible = true;



}



否则if(i == 2)

{

RadioButton1.Visible = true;

RadioButton2.Visible = true;

RadioButton3.Visible = true;

RadioButton4.Visible = true;

RadioButton5.Visible = true;

CheckBox1.Visible = false;

CheckBox2.Visible = false;

CheckBox3。 Visible = false;

CheckBox4.Visible = false;

CheckBox5.Visible = false;



}

else if(i == 3)

{

RadioButton1.Visible = false;

RadioButton2.Visible = false;

RadioButton3.Visible = false;

RadioButton4.Visible = false;

RadioButton5.Visible = false;

CheckBox1.Visible = true;

CheckBox2.Visible = true;

CheckBox3.Visible = true;

CheckBox4.Visible = true;

CheckBox5.Visible = true ;



}

其他

{

RadioButton1.Visible = true;

RadioButton2.Visible = true;

RadioButton3.Visible = true;

RadioButton4.Visible = true;

RadioButton5.Visible = true;

CheckBox1.Visible = false;

CheckBox2.Visible = false;

CheckBox3.Visible = false;

CheckBox4.Visible = false;

CheckBox5.Visible = false;





}



}







protected void previous_Click(object sender ,EventArgs e)

{

//Con.Open();

// Cmd = new SqlCommand(select * from student where Question ='+ TextBox1.Text +',Con);

// dr = Cmd.ExecuteReader();

// while(dr.Read( ))

// {

// Label7.Text = dr [Q_ID]。ToString();

//}

//Con.Close();

TextBox2.Text =(int.Parse(TextBox2.Text) - 1).ToString();

i = Convert.ToInt32(TextBox2.Text);



// Label7.Text = ds.Tables [0] .Rows [i] [Q_ID] .ToString();



TextBox1.Text = ds.Tables [0] .Rows [i] [Question]。ToString();

Label2.Text = ds.Tables [0] .Rows [i] [Choice1]。ToString();

Label3.Text = ds.Tables [0] .Rows [ I] [ 选择2]。的ToS tring();

Label4.Text = ds.Tables [0] .Rows [i] [Choice3]。ToString();

Label5.Text = ds .Tables [0] .Rows [i] [Choice4]。ToString();

Label6.Text = ds.Tables [0] .Rows [i] [Choice5]。ToString ();







if(i == 1)

{

RadioButton1.Visible = false;

RadioButton2.Visible = false;

RadioButton3.Visible = false;

RadioButton4.Visible = false;

RadioButton5.Visible = false;

CheckBox1.Visible = true;

CheckBox2.Visible = true;

CheckBox3.Visible = true;

CheckBox4.Visible = true;

CheckBox5.Visible = true;



}

否则if(i == 2)

{

RadioButton1.Visible = true;

RadioButton2.Visible = true;

RadioButton3.Visible = true;

RadioButton4.Visible = true;

RadioButton5.Visible = true;

CheckBox1.Visible = false;

CheckBox2.Visible = false;

CheckBox3.Visible = false;

CheckBox4。 Visible = false;

CheckBox5.Visible = false;



}

else if(i == 3 )

{

RadioButton1.Visible = false;

RadioButton2.Visible = false;

RadioButton3.Visible = false;

RadioButton4.Visible = false;

RadioButton5.Visible = false;

CheckBox1.Visible = true;

CheckBox2.Visible = true;

CheckBox3.Visible = true;

CheckBox4.Visible = true;

CheckBox5.Visible = true;

}

else

{

RadioButton1.Visible = true;

RadioButton2.Visible = true;

RadioButton3.Visible = true;

RadioButton4.Visible = true;

RadioButton5.Visible = true;

CheckBox1.Visible = false;

CheckBox2.Visible = false;

CheckBox3.Visible = false;

CheckBox4.Visible = false;

CheckBox5.Visible = false;

}





}



protected void Submit_Click(object sender,EventArgs e)

{







string cs =Data Source = DESKTOP-Q69PRF4; Initial Catalog = new; Integrated Security = True;

SqlConnection con = new SqlConnect离子(cs);

if(con.State == ConnectionState.Open)

{

con.Close();

}

//con.Open();

// Cmd = new SqlCommand(select * from student where Question ='+ TextBox1。文字+',con);

// SqlDataReader r = Cmd.ExecuteReader();

//// SqlDatareader dr = Cmd.ExecuteReader();

// while(r.Read())

// {

// Label7.Text = r [Q_ID]。ToString ();

//}

//con.Close();

con.Open();



if(RadioButton1.Visible == true)

{

if(RadioButton1.Checked)

{

string query =插入CBE(Q_ID,StID,Answer)值('+ Label7.Text +','+ Session [new] +',' + lblA.Text +');

SqlCommand cmd = ne w SqlCommand(query,con);

cmd.ExecuteNonQuery();



}

else if( RadioButton2.Checked)

{

string query =插入CBE(Q_ID,StID,Answer)值('+ Label7.Text +','+ Session [new] +','+ lblB.Text +');

SqlCommand cmd = new SqlCommand(query,con);

cmd .ExecuteNonQuery();



}

else if(RadioButton3.Checked)

{

string query =插入CBE(Q_ID,StID,Answer)值('+ Label7.Text +','+ Session [new] +','+ lblC.Text + ');

SqlCommand cmd = new SqlCommand(query,con);

cmd.ExecuteNonQuery();



}

else if(RadioButton4.Checked)

{

string query =插入CBE(Q_ID,StID,Answer)值('+ Label7.Text +','+ Session [new] +','+ lblD.Text +');

SqlCommand cmd = new SqlCommand(query,con);

cmd.ExecuteNonQuery();



}

else if(RadioButton5.Checked)

{

string query =insert into CBE(Q_ID ,StID,Answer)值('+ Label7.Text +','+ Session [new] +','+ lblE.Text +');

SqlCommand cmd = new SqlCommand(query,con);

cmd.ExecuteNonQuery();



}

否则

{

if(CheckBox1.Checked)

{

string query =insert into CBE(Q_ID ,StID,Ans1)值('+ Label7.Text +','+ Session [new] +' ,'+ lblA.Text +');

SqlCommand cmd = new SqlCommand(query,con);

cmd.ExecuteNonQuery();



}

否则if(CheckBox2.Checked)

{

string query =insert进入CBE(Q_ID,StID,Ans2)值('+ Label7.Text +','+ Session [new] +','+ lblB.Text +');

SqlCommand cmd = new SqlCommand(query,con);

cmd.ExecuteNonQuery();



}

else if(CheckBox3.Checked)

{

string query =插入CBE(Q_ID,StID,Ans3)值('+ Label7.Text +','+ Session [new] +','+ lblC.Text +');

SqlCommand cmd = new SqlCommand(query,con);

cmd.ExecuteNonQuery() ;



}

否则if(CheckBox4.Checked)

{

string query =插入CBE(Q_ID,StID,Ans4)值('+ Label7.Text +','+ Session [new] +','+ lblD.Text +') ;

SqlCommand cmd = new SqlCommand(query,con);

cmd.ExecuteNonQuery();



}

否则if(CheckBox5.Checked)

{

string query =插入CBE(Q_ID,StID,Ans5)值(' + Label7.Text +','+ Session [new] +','+ lblE.Text +');

SqlCommand cmd = new SqlCommand(查询, con);

cmd.ExecuteNonQuery();



}

}

}

con.Close();



con.Open();

Cmd = new SqlCommand(select Student from Student where Q_Id ='+ Label7.Text +',con);

SqlDataReader dr = Cmd.ExecuteReader();

while(dr.Read())

{

Label8.Text = dr [Solution] .ToString();



}

con.Close();

//con.Open( );

//字符串qr =更新CBE设置答案='+ Label8.Text +'其中Q_ID ='+ Label7.Text +';



// Cmd =新的SqlCommand(qr,con);



// dr = Cmd.ExecuteReader();



// while(dr.Read())

// {

// lblans.Text = dr [ 答案]。ToString();

//}

//con.Close();

con.Open();

Cmd = new SqlCommand(select * from CBE where Q_ID ='+ Label7.Text +',con);



dr = Cmd.ExecuteReader();



while(dr.Read())

{

if(RadioButton1.Visible == true)

{

if(RadioButton1.Checked)

{

lblcheck.Text = dr [Answer]。ToString();

}

else if(RadioButton2.Checked)

{

lblcheck.Text = dr [Answer]。ToString();



}

else if(RadioButton3.Checked)

{

lblcheck.Text = dr [Answer]。ToString();



}

否则如果(RadioButton4.Checked)

{

lblcheck.Text = dr [Answer] .ToString();



}

else if(RadioButton5.Checked)

{

lblcheck.Text = dr [Answer]。ToString();



}

其他

{

lblcheck.Text =;

}

}

其他

{

if(CheckBox1.Checked)

{

lblcheck.Text = dr [Answer]。ToString();

}

else if(CheckBox2.Checked)< br $>
{

lblcheck.Text = dr [Answer]。ToString();



}

else if(CheckBox3.Checked)

{

lblcheck.Text = dr [Answer]。ToString();



}

否则如果(CheckBox4.Checked)

{

lblcheck.Text = dr [Answer]。ToString();



}

else if(CheckBox5.Checked)

{

lblcheck.Text = dr [Answer]。ToString ();



}

else

{

lblcheck.Text = ;

}

}



}

con.Close( );

con.Open();

Cmd = new SqlCommand(从学生中选择TotalMarks,其中Q_ID ='+ Label7.Text +',con) ;

dr = Cmd.ExecuteReader();

while(dr.Read())

{

lblques.Text = dr [TotalMarks]。ToString();

}

con.Close();

con.Open();

if(lblcheck.Text == Label8.Text)

{



Cmd = new SqlCommand(Update CBE Set StMarks ='+ lblques.Text +'where Q_ID ='+ Label7.Text +',con);



}

else if(lblcheck.Text!= Label8.Text)

{

Cmd = new SqlCommand(更新CBE设置StMarks ='+ lblm.Text +'其中Q_ID ='+ Label7.Text +',con);



}

else

lblques.Text =请选择至少一个选项;

Cmd.ExecuteNonQuery();



con.Close();





}





无效清晰()



{



RadioButton1.Checked = false;

RadioButton2.Checked = false;

RadioButton3.Checked = false;

RadioButton4.Checked = false;

RadioButton5.Checked = false;

CheckBox1.Checked = false;

CheckBox2.Checked = false;

CheckBox3.Checked = false;

CheckBox4.Checked = false;

CheckBox5.Checked = false;

}









}

I am making a web application that have student's record and test papers. I want to reload the data from database if computer will be off suddenly(may be due to light issue or laptop battery issue) while student is doing his/her test. All marked answer should be display again when computer turn on again. I can't understand how to do it. Below is my code.
Can anyone tell me what to do.

What I have tried:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Sql;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{

SqlConnection Con;
SqlCommand Cmd;
SqlDataAdapter da;
SqlDataReader dr;
DataSet ds;
DataTable dt = new DataTable();
int recordcount = 0;
int i = 0;
// string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{

Con = new SqlConnection("Data Source=DESKTOP-Q69PRF4;Initial Catalog=new;Integrated Security=True");
Cmd = new SqlCommand("SELECT * FROM student where category='"+Session["test"]+ "'", Con);
da = new SqlDataAdapter(Cmd);
ds = new DataSet("student");

Con.Open();
this.da.Fill(ds, "student");
Cmd = new SqlCommand("select * from student where Question='" + TextBox1.Text + "'", Con);
dr = Cmd.ExecuteReader();
while(dr.Read())
{
Label7.Text = dr["Q_ID"].ToString();
lblmarks.Text = dr["TotalMarks"].ToString();

}


recordcount = ds.Tables[0].Rows.Count;
Response.Write("Record Count : " + recordcount);


if (recordcount > i)
{
TextBox1.Text = ds.Tables[0].Rows[i]["Question"].ToString();
Label2.Text = ds.Tables[0].Rows[i]["Choice1"].ToString();
Label3.Text = ds.Tables[0].Rows[i]["Choice2"].ToString();
Label4.Text = ds.Tables[0].Rows[i]["Choice3"].ToString();
Label5.Text = ds.Tables[0].Rows[i]["Choice4"].ToString();
Label6.Text = ds.Tables[0].Rows[i]["Choice5"].ToString();
}
else
{
Response.Write("There are no records in this category");
}
if (i == 0)
{
RadioButton1.Visible = true;
RadioButton2.Visible = true;
RadioButton3.Visible = true;
RadioButton4.Visible = true;
RadioButton5.Visible = true;
CheckBox1.Visible = false;
CheckBox2.Visible = false;
CheckBox3.Visible = false;
CheckBox4.Visible = false;
CheckBox5.Visible = false;

}
dr.Read();


Con.Close();

}


protected void Button2_Click(object sender, EventArgs e)
{

//Con.Open();
//Cmd = new SqlCommand("select * from student where Question='" + TextBox1.Text + "'", Con);
//dr = Cmd.ExecuteReader();
//while (dr.Read())
//{
// Label7.Text = dr["Q_ID"].ToString();
//}
//Con.Close();

TextBox2.Text = (int.Parse(TextBox2.Text) + 1).ToString();

i = Convert.ToInt32(TextBox2.Text);


TextBox1.Text = ds.Tables[0].Rows[i]["Question"].ToString();
Label2.Text = ds.Tables[0].Rows[i]["Choice1"].ToString();
Label3.Text = ds.Tables[0].Rows[i]["Choice2"].ToString();
Label4.Text = ds.Tables[0].Rows[i]["Choice3"].ToString();
Label5.Text = ds.Tables[0].Rows[i]["Choice4"].ToString();
Label6.Text = ds.Tables[0].Rows[i]["Choice5"].ToString();


if (i == 1)
{
RadioButton1.Visible = false;
RadioButton2.Visible = false;
RadioButton3.Visible = false;
RadioButton4.Visible = false;
RadioButton5.Visible = false;
CheckBox1.Visible = true;
CheckBox2.Visible = true;
CheckBox3.Visible = true;
CheckBox4.Visible = true;
CheckBox5.Visible = true;

}

else if (i == 2)
{
RadioButton1.Visible = true;
RadioButton2.Visible = true;
RadioButton3.Visible = true;
RadioButton4.Visible = true;
RadioButton5.Visible = true;
CheckBox1.Visible = false;
CheckBox2.Visible = false;
CheckBox3.Visible = false;
CheckBox4.Visible = false;
CheckBox5.Visible = false;

}
else if (i == 3)
{
RadioButton1.Visible = false;
RadioButton2.Visible = false;
RadioButton3.Visible = false;
RadioButton4.Visible = false;
RadioButton5.Visible = false;
CheckBox1.Visible = true;
CheckBox2.Visible = true;
CheckBox3.Visible = true;
CheckBox4.Visible = true;
CheckBox5.Visible = true;

}
else
{
RadioButton1.Visible = true;
RadioButton2.Visible = true;
RadioButton3.Visible = true;
RadioButton4.Visible = true;
RadioButton5.Visible = true;
CheckBox1.Visible = false;
CheckBox2.Visible = false;
CheckBox3.Visible = false;
CheckBox4.Visible = false;
CheckBox5.Visible = false;


}

}



protected void previous_Click(object sender, EventArgs e)
{
//Con.Open();
//Cmd = new SqlCommand("select * from student where Question='" + TextBox1.Text + "'", Con);
//dr = Cmd.ExecuteReader();
//while (dr.Read())
//{
// Label7.Text = dr["Q_ID"].ToString();
//}
//Con.Close();
TextBox2.Text = (int.Parse(TextBox2.Text) - 1).ToString();
i = Convert.ToInt32(TextBox2.Text);

// Label7.Text = ds.Tables[0].Rows[i]["Q_ID"].ToString();

TextBox1.Text = ds.Tables[0].Rows[i]["Question"].ToString();
Label2.Text = ds.Tables[0].Rows[i]["Choice1"].ToString();
Label3.Text = ds.Tables[0].Rows[i]["Choice2"].ToString();
Label4.Text = ds.Tables[0].Rows[i]["Choice3"].ToString();
Label5.Text = ds.Tables[0].Rows[i]["Choice4"].ToString();
Label6.Text = ds.Tables[0].Rows[i]["Choice5"].ToString();



if (i == 1)
{
RadioButton1.Visible = false;
RadioButton2.Visible = false;
RadioButton3.Visible = false;
RadioButton4.Visible = false;
RadioButton5.Visible = false;
CheckBox1.Visible = true;
CheckBox2.Visible = true;
CheckBox3.Visible = true;
CheckBox4.Visible = true;
CheckBox5.Visible = true;

}
else if (i == 2)
{
RadioButton1.Visible = true;
RadioButton2.Visible = true;
RadioButton3.Visible = true;
RadioButton4.Visible = true;
RadioButton5.Visible = true;
CheckBox1.Visible = false;
CheckBox2.Visible = false;
CheckBox3.Visible = false;
CheckBox4.Visible = false;
CheckBox5.Visible = false;

}
else if (i == 3)
{
RadioButton1.Visible = false;
RadioButton2.Visible = false;
RadioButton3.Visible = false;
RadioButton4.Visible = false;
RadioButton5.Visible = false;
CheckBox1.Visible = true;
CheckBox2.Visible = true;
CheckBox3.Visible = true;
CheckBox4.Visible = true;
CheckBox5.Visible = true;
}
else
{
RadioButton1.Visible = true;
RadioButton2.Visible = true;
RadioButton3.Visible = true;
RadioButton4.Visible = true;
RadioButton5.Visible = true;
CheckBox1.Visible = false;
CheckBox2.Visible = false;
CheckBox3.Visible = false;
CheckBox4.Visible = false;
CheckBox5.Visible = false;
}


}

protected void Submit_Click(object sender, EventArgs e)
{



string cs = "Data Source=DESKTOP-Q69PRF4;Initial Catalog=new;Integrated Security=True";
SqlConnection con = new SqlConnection(cs);
if (con.State == ConnectionState.Open)
{
con.Close();
}
//con.Open();
//Cmd = new SqlCommand("select * from student where Question='" + TextBox1.Text + "'", con);
//SqlDataReader r = Cmd.ExecuteReader();
////SqlDatareader dr = Cmd.ExecuteReader();
//while (r.Read())
//{
// Label7.Text = r["Q_ID"].ToString();
//}
//con.Close();
con.Open();

if (RadioButton1.Visible == true)
{
if (RadioButton1.Checked)
{
string query = "insert into CBE(Q_ID,StID,Answer)values('" + Label7.Text + "','" + Session["new"] + "','" + lblA.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();

}
else if (RadioButton2.Checked )
{
string query = "insert into CBE(Q_ID,StID,Answer)values('" + Label7.Text + "','" + Session["new"] + "','" + lblB.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();

}
else if (RadioButton3.Checked )
{
string query = "insert into CBE(Q_ID,StID,Answer)values('" + Label7.Text + "','" + Session["new"] + "','" + lblC.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();

}
else if (RadioButton4.Checked )
{
string query = "insert into CBE(Q_ID,StID,Answer)values('" + Label7.Text + "','" + Session["new"] + "','" + lblD.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();

}
else if (RadioButton5.Checked )
{
string query = "insert into CBE(Q_ID,StID,Answer)values('" + Label7.Text + "','" + Session["new"] + "','" + lblE.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();

}
else
{
if (CheckBox1.Checked)
{
string query = "insert into CBE(Q_ID,StID,Ans1)values('" + Label7.Text + "','" + Session["new"] + "','" + lblA.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();

}
else if (CheckBox2.Checked)
{
string query = "insert into CBE(Q_ID,StID,Ans2)values('" + Label7.Text + "','" + Session["new"] + "','" + lblB.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();

}
else if (CheckBox3.Checked)
{
string query = "insert into CBE(Q_ID,StID,Ans3)values('" + Label7.Text + "','" + Session["new"] + "','" + lblC.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();

}
else if (CheckBox4.Checked)
{
string query = "insert into CBE(Q_ID,StID,Ans4)values('" + Label7.Text + "','" + Session["new"] + "','" + lblD.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();

}
else if (CheckBox5.Checked)
{
string query = "insert into CBE(Q_ID,StID,Ans5)values('" + Label7.Text + "','" + Session["new"] + "','" + lblE.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();

}
}
}
con.Close();

con.Open();
Cmd = new SqlCommand("select Solution from Student where Q_Id='"+Label7.Text+"'",con);
SqlDataReader dr = Cmd.ExecuteReader();
while(dr.Read())
{
Label8.Text = dr["Solution"].ToString();

}
con.Close();
//con.Open();
//string qr = "Update CBE Set Answer='" + Label8.Text + "'where Q_ID='"+Label7.Text+"'";

//Cmd = new SqlCommand(qr,con);

//dr = Cmd.ExecuteReader();

//while (dr.Read())
//{
// lblans.Text = dr["Answer"].ToString();
//}
//con.Close();
con.Open();
Cmd = new SqlCommand("select * from CBE where Q_ID='" + Label7.Text + "'", con);

dr = Cmd.ExecuteReader();

while (dr.Read())
{
if (RadioButton1.Visible == true)
{
if (RadioButton1.Checked)
{
lblcheck.Text = dr["Answer"].ToString();
}
else if (RadioButton2.Checked)
{
lblcheck.Text = dr["Answer"].ToString();

}
else if (RadioButton3.Checked)
{
lblcheck.Text = dr["Answer"].ToString();

}
else if (RadioButton4.Checked)
{
lblcheck.Text = dr["Answer"].ToString();

}
else if (RadioButton5.Checked)
{
lblcheck.Text = dr["Answer"].ToString();

}
else
{
lblcheck.Text = "";
}
}
else
{
if (CheckBox1.Checked)
{
lblcheck.Text = dr["Answer"].ToString();
}
else if (CheckBox2.Checked)
{
lblcheck.Text = dr["Answer"].ToString();

}
else if (CheckBox3.Checked)
{
lblcheck.Text = dr["Answer"].ToString();

}
else if (CheckBox4.Checked)
{
lblcheck.Text = dr["Answer"].ToString();

}
else if (CheckBox5.Checked)
{
lblcheck.Text = dr["Answer"].ToString();

}
else
{
lblcheck.Text = "";
}
}

}
con.Close();
con.Open();
Cmd = new SqlCommand("Select TotalMarks from Student where Q_ID='" + Label7.Text + "'", con);
dr = Cmd.ExecuteReader();
while(dr.Read())
{
lblques.Text = dr["TotalMarks"].ToString();
}
con.Close();
con.Open();
if (lblcheck.Text == Label8.Text)
{

Cmd = new SqlCommand("Update CBE Set StMarks='" + lblques.Text + "'where Q_ID='" + Label7.Text + "'", con);

}
else if (lblcheck.Text != Label8.Text)
{
Cmd = new SqlCommand("Update CBE Set StMarks='" + lblm.Text + "'where Q_ID='" + Label7.Text + "'", con);

}
else
lblques.Text = "Please Select at least one option";
Cmd.ExecuteNonQuery();

con.Close();


}


void clear()

{

RadioButton1.Checked = false;
RadioButton2.Checked = false;
RadioButton3.Checked = false;
RadioButton4.Checked = false;
RadioButton5.Checked = false;
CheckBox1.Checked = false;
CheckBox2.Checked = false;
CheckBox3.Checked = false;
CheckBox4.Checked = false;
CheckBox5.Checked = false;
}




}

推荐答案

你认为你有更大的问题 - 只是把你所有的代码都倾倒在我们身上并希望我们理清你的意思不是那个 - 这只是粗鲁的。



忽略你目前遇到的问题,先解决这个问题:

永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

You have bigger problems that you think - and just dumping all your code on us and expecting us to sort out what the heck you mean is not that - it's just rude.

Ignore the problem you have at the moment, and fix this first:
Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期做备份,不是吗?

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?


这篇关于如何从数据库重新加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆