求高手详细的标注一下这段代码,越详细越好。
private DataSet GetDataSet(){
DataSet ds = new DataSet();
String dbProvider = ConfigurationManager.AppSettings["ExclDbProvider"];
String exclPath = ConfigurationManager.AppSettings["exclPath"];
exclPath = Server.MapPath(exclPath);
String iasmVersion = ConfigurationManager.AppSettings["IasmVersion"];
String sheetName = ConfigurationManager.AppSettings["SheetName"];
String exclConStr = String.Format("Provider={0};Data Source={1};Extended Properties='{2};';", dbProvider, exclPath, iasmVersion);
String exclSqlCmdStr = String.Format("SELECT * FROM [{0}]", sheetName);
OleDbDataAdapter oleDA = new OleDbDataAdapter(exclSqlCmdStr, exclConStr);
oleDA.Fill(ds, sheetName);
SaveToDataBase(ds);
return ds;
}
private void BindGridView()
{
String tblName = ConfigurationManager.AppSettings["SheetName"];
gv_ExcelData.DataSource = GetDataSet().Tables[tblName];
gv_ExcelData.DataBind();
}
private void SaveToDataBase(DataSet ds)
{
String sqlConStr = ConfigurationManager.ConnectionStrings["dbconstr"].ConnectionString;
String dbTblName = ConfigurationManager.AppSettings["DbTableName"];
String col1 = "编号";
String col2 = "测试用例";
String col3 = "测试环境";
String col4 = "测试软件";
String col5 = "缺陷类型";
String col6 = "缺陷描述";
String col7 = "严重程度";
String col8 = "测试人员";
String col9 = "测试日期";
String para1 = "@编号";
String para2 = "@测试用例";
String para3 = "@测试环境";
String para4 = "@测试软件";
String para5 = "@缺陷类型";
String para6 = "@缺陷描述";
String para7 = "@严重程度";
String para8 = "@测试人员";
String para9 = "@测试日期";
String insSqlCmdStr = String.Format("INSERT INTO {0} ({1},{2},{3},{4},{5},{6},{7},{8},{9}) VALUES({10},{11},{12},{13},{14},{15},{16},{17},{18})",
dbTblName, col1, col2, col3, col4, col5, col6, col7, col8, col9, para1, para2, para3,
para4, para5, para6, para7, para8, para9);
String delSqlCmdStr = String.Format("DELETE FROM {0}", dbTblName);
try
{
SqlConnection sqlCon = new SqlConnection(sqlConStr);
sqlCon.Open();
SqlCommand delSqlCmd = new SqlCommand(delSqlCmdStr, sqlCon);
delSqlCmd.ExecuteNonQuery();
String tblName = ConfigurationManager.AppSettings["SheetName"];
foreach (DataRow row in ds.Tables[tblName].Rows)
{
SqlCommand insSqlCmd = new SqlCommand(insSqlCmdStr, sqlCon);
insSqlCmd.Parameters.Add(new SqlParameter(para1, row[col1].ToString()));
insSqlCmd.Parameters.Add(new SqlParameter(para2, row[col2].ToString()));
insSqlCmd.Parameters.Add(new SqlParameter(para3, row[col3].ToString()));
insSqlCmd.Parameters.Add(new SqlParameter(para4, row[col4].ToString()));
insSqlCmd.Parameters.Add(new SqlParameter(para5, row[col5].ToString()));
insSqlCmd.Parameters.Add(new SqlParameter(para6, row[col6].ToString()));
insSqlCmd.Parameters.Add(new SqlParameter(para7, row[col7].ToString()));
insSqlCmd.Parameters.Add(new SqlParameter(para8, row[col8].ToString()));
insSqlCmd.Parameters.Add(new SqlParameter(para9, Convert.ToDateTime(row[col9].ToString())));
insSqlCmd.ExecuteNonQuery();
}
sqlCon.Close();
}
catch (Exception e)
{
throw new Exception("访问数据库发生异常!" + e);
}
}
}
新手学C#,哪位热心的大哥大姐给个详细的标注。 --------------------编程问答-------------------- GetDataSet()//通过系统的配置文件,查excel内容,返回一个DataSet类型
,这个方法里又调用了SaveToDataBase()方法
SaveToDataBase()//先删除数据库所有数据,再以excel返回的datatable填充
BindGridView()//将excel内容,呈现名为gv_ExcelData的gridview中
整个逻辑就是通过excel取出值来,然后填充进数据库,同时展现出来。
PS:
那些AppSettings不用管它,都是配置字节,去config一看就知道了。 --------------------编程问答-------------------- +1.
--------------------编程问答-------------------- 很详细了! --------------------编程问答--------------------
+1 --------------------编程问答-------------------- 哇,感谢一楼六点多就起来回答问题。
我想继续问一下。
这段
String insSqlCmdStr = String.Format("INSERT INTO {0} ({1},{2},{3},{4},{5},{6},{7},{8},{9}) VALUES({10},{11},{12},{13},{14},{15},{16},{17},{18})",
dbTblName, col1, col2, col3, col4, col5, col6, col7, col8, col9, para1, para2, para3,
para4, para5, para6, para7, para8, para9);
还有
foreach (DataRow row in ds.Tables[tblName].Rows)
{
SqlCommand insSqlCmd = new SqlCommand(insSqlCmdStr, sqlCon);
insSqlCmd.Parameters.Add(new SqlParameter(para1, row[col1].ToString()));
insSqlCmd.Parameters.Add(new SqlParameter(para2, row[col2].ToString()));
insSqlCmd.Parameters.Add(new SqlParameter(para3, row[col3].ToString()));
insSqlCmd.Parameters.Add(new SqlParameter(para4, row[col4].ToString()));
insSqlCmd.Parameters.Add(new SqlParameter(para5, row[col5].ToString()));
insSqlCmd.Parameters.Add(new SqlParameter(para6, row[col6].ToString()));
insSqlCmd.Parameters.Add(new SqlParameter(para7, row[col7].ToString()));
insSqlCmd.Parameters.Add(new SqlParameter(para8, row[col8].ToString()));
insSqlCmd.Parameters.Add(new SqlParameter(para9, Convert.ToDateTime(row[col9].ToString())));
insSqlCmd.ExecuteNonQuery();
}
是实现什么。
--------------------编程问答--------------------
插入啊,上面不是有insert语句嘛,下面是一个遍历,遍历SheetName表里面的行并添加
补充:.NET技术 , C#