【求助】关于Excel内容导入数据库时,SQL语句与外键约束发生冲突。在线跪等。
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["mystring"]);
protected void Page_Load(object sender, EventArgs e)
{
}
public DataSet ExecleDs(string filenameurl, string table)
{
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
OleDbConnection con = new OleDbConnection(strConn);
con.Open();
DataSet ds = new DataSet();
OleDbDataAdapter odda = new OleDbDataAdapter("select * from [Sheet1$]", con);
odda.Fill(ds, table);
return ds;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile == false)
{
Response.Write("<script>alert('请您选择Excel文件')</script> ");
return;
}
string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();
if (IsXls != ".xls")
{
Response.Write("<script>alert('只可以选择Excel文件')</script>");
return;
}
conn.Open();
string filename = DateTime.Now.ToString("yyyymmddhhMMss") + FileUpload1.FileName;
string savePath = Server.MapPath(("~\\upfiles\\") + filename);
FileUpload1.SaveAs(savePath);
DataSet ds = ExecleDs(savePath, filename);
DataRow[] dr = ds.Tables[0].Select();
int rowsnum = ds.Tables[0].Rows.Count;
if (rowsnum == 0)
{
Response.Write("<script>alert('Excel表为空表,无数据!')</script>");
}
else
{
for (int i = 0; i < dr.Length; i++)
{
string student_xh = dr[i]["学号"].ToString();//日期 excel列名【名称不能变,否则就会出错】
string student_xm = dr[i]["姓名"].ToString();//编号 列名 以下类似
string student_jg = dr[i]["籍贯"].ToString();
string student_xb = dr[i]["性别"].ToString();
string student_sr = dr[i]["出生日期"].ToString();
string student_bj = dr[i]["班级号"].ToString();
string student_dh = dr[i]["电话号码"].ToString();
string student_xf = dr[i]["已修学分"].ToString();
string student_rx = dr[i]["入学时间"].ToString();
string student_zz = dr[i]["家庭住址"].ToString();
string student_mm = dr[i]["密码"].ToString();
string student_bz = dr[i]["备注"].ToString();
string sqlcheck = "select count(*) from student where 学号 = ' " + student_xh + "'";
SqlCommand sqlcmd = new SqlCommand(sqlcheck, conn);
int count = Convert.ToInt32(sqlcmd.ExecuteScalar());
if (count < 1)
{
string insertstr = "insert into student (学号,姓名,籍贯,性别,出生日期,班级号,电话号码,已修学分,入学时间,家庭住址,密码,备注) values('" + student_xh + "','" + student_xm + "','" + student_jg + "','" + student_xb + "','" + student_sr + "','" + student_bj + "','" + student_dh + "','" + student_xf + "','" + student_rx + "','" + student_zz + "','" + student_mm + "','" + student_bz + "')";
SqlCommand cmd = new SqlCommand(insertstr, conn);
try
{
int a = cmd.ExecuteNonQuery();
}
catch (MembershipCreateUserException ex)
{
Response.Write("<script>alert('导入内容:" + ex.Message + "')</script>");
}
}
else
{
Response.Write("<script>alert('内容重复!禁止导入');location='DateInfo.aspx'</script></script> ");
continue;
}
}
Response.Write("<script>alert('Excle表导入成功!');location='DateInfo.aspx'</script>");
}
conn.Close();
}
在向student表插入时,外键冲突,与另外一张表class中的主键 班级号 发生冲突 代码要怎么判断呢? --------------------编程问答-------------------- 把班级号全取出来 存进 list 遍历excel数据时 判断 list中是否包含这个班级号
如果list中不包含 跳出循环 继续下一条
补充:.NET技术 , C#