我設計的數據表testdetail為:
GridView中有數據,形式為
我現在想把GridView中的Label3的內容(已綁定了數值)存到testdetail的typeid中,
我使用的方法是
DataBase db = new DataBase();
foreach (GridViewRow dr in GridView2.Rows)//保存試卷單選題信息
{
string single = "insert into testdetail(examinationid,typekind,typeid,score) values(" + afterID + ",'單選題'," + int.Parse(((Label)dr.FindControl("Label3")).Text) + "," + int.Parse(txtSingleFen.Text) + ")";
db.Insert(single);
}
Insert是我已經在Database.cs中定義了的:
public int Insert(string XSqlString)
{
int Count = -1;
Open();
SqlCommand cmd = new SqlCommand(XSqlString, Connection);
Count = cmd.ExecuteNonQuery();
Close();
return Count;
}
運行之後發現testdetail中這些數據並沒有被存儲進去,不知道是為什麼...
由於初學也不知道自己寫的是不是對的,請大家幫忙指導一下,非常感謝!
這個代碼較完整,可以參考:
int Djls = 0;
ds = new DataSet();//DataSet
string sql = "select gch,tdh,bh,barcode,cc,sl,ys,bzl,zc,printtime from printdata";
da = new SqlDataAdapter(sql, cn);//SqlDataAdapter
da.Fill(ds, "printdata");
SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(da);
for (int i = 0; i < gridView1.RowCount - 1; i++)
{
if (gridView1.GetRowCellValue(i, gridView1.Columns[0]).ToString() == "" || gridView1.GetRowCellValue(i, gridView1.Columns[0]).ToString() == null)
{
MessageBox.Show("工程號不能為空,第" + (i + 1) + "行記錄", "信息提示");
return;
}
}
for (int i = 0; i < gridView1.RowCount - 1; i++)
{
string sql2 = "select gch,tdh,bh,barcode,cc,sl,ys,bzl,zc,printtime from printdata where gch='" + gridView1.GetRowCellValue(i, gridView1.Columns[0]) + "' and tdh='" + gridView1.GetRowCellValue(i, gridView1.Columns[1]) + "' and bh='" + gridView1.GetRowCellValue(i, gridView1.Columns[2]) + "'";
DataSet ds2 = new DataSet();
SqlCommand sqlcmd2 = new SqlCommand(sql2, cn);
SqlDataAdapter da2 = new SqlDataAdapter(sqlcmd2);
da2.Fill(ds2, "printdata");
DataRow dr = ds.Tables["printdata"].NewRow();
if (ds2.Tables[0].Rows.Count <= 0)
{
dr["gch"] =gridView1.GetRowCellValue(i, gridView1.Columns[0]).ToString();
dr["tdh"] = gridView1.GetRowCellValue(i, gridView1.Columns[1]).ToString();
dr["bh"] = gridView1.GetRowCellValue(i, gridView1.Columns[2]).ToString();
dr["barcode"] = gridView1.GetRowCellValue(i, gridView1.Columns[0]).ToString() + "-" + gridView1.GetRowCellValue(i, gridView1.Columns[1]).ToString() + "-" + gridView1.GetRowCellValue(i, gridView1.Columns[2]).ToString();
dr["cc"] = gridView1.GetRowCellValue(i, gridView1.Columns[3]).ToString();
dr["sl"] = gridView1.GetRowCellValue(i, gridView1.Columns[4]).ToString();
dr["ys"] = gridView1.GetRowCellValue(i, gridView1.Columns[5]).ToString();
dr["bzl"] = gridView1.GetRowCellValue(i, gridView1.Columns[6]).ToString();
dr["zc"] = gridView1.GetRowCellValue(i, gridView1.Columns[7]).ToString();
dr["printtime"] = DateTime.Now.ToString();
ds.Tables[0].Rows.Add(dr);
Djls++;
}
}
da.Update(ds.Tables["printdata"]);
MessageBox.Show("共有『" + (Djls) + "』記錄數被導入");