excel导入SQL数据库,的Button事件问题excel导入SQL数据库,有2个按钮事件,Button1_Click把数据在表中显示下
excel导入SQL数据库,的Button事件问题
excel导入SQL数据库,有2个按钮事件,Button1_Click把数据在表中显示下,不对数据库进行操作,Button2_Click对数据库进行操作,但是总是出错,无效参数,如果单独执行Button2_Click对数据库进行操作是可以的,执行Button1_Click也可以。但是先Button1_Click在Button2_Click就无效参数了。是不是执行2次
C# codestring sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + FileUpload1.PostedFile.FileName.ToString() + ";" + "Extended Properties=\"Excel 8.0;\""; OleDbConnection objConn = new OleDbConnection(sConnectionString); objConn.Open(); OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn); OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(); objAdapter1.SelectCommand = objCmdSelect; DataSet objDataset1 = new DataSet(); objAdapter1.Fill(objDataset1);
不行啊,那位高手帮解决下,2天了
SqlConnection conn = new SqlConnection("Data Source=.;Database=hhxy_gzcx;User ID=sa;PWD=11");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + FileUpload1.PostedFile.FileName.ToString() + ";" + "Extended Properties=\"Excel 8.0;\"";
OleDbConnection objConn = new OleDbConnection(sConnectionString);
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataSet objDataset1 = new DataSet();
objAdapter1.Fill(objDataset1);
GridView1.DataSource = objDataset1;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
string sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + FileUpload1.PostedFile.FileName.ToString() + ";" + "Extended Properties=\"Excel 8.0;\"";
OleDbConnection objConn = new OleDbConnection(sConnectionString);
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataSet objDataset1 = new DataSet();
objAdapter1.Fill(objDataset1);
DataTable dt = objDataset1.Tables[0];
DataView myView = new DataView(dt);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO user_table values(@姓名,@密码)";
int count=0;
try
{
foreach (DataRowView myDrv in myView)
{
count++;
if (conn.State.ToString() != "Closed")
conn.Close();
cmd.Parameters.Clear();
SqlParameter 姓名 = cmd.Parameters.Add("@姓名", SqlDbType.Char);
SqlParameter 密码 = cmd.Parameters.Add("@密码", SqlDbType.Char);
姓名.Direction = ParameterDirection.Input;
密码.Direction = ParameterDirection.Input;
姓名.Value = myDrv[0].ToString().Trim();
密码.Value = myDrv[1].ToString().Trim();
conn.Open();
cmd.ExecuteNonQuery();//写入SQL数据库
}
Page.Response.Write("<script>alert('导入成功')</script>");
}
catch (SqlException ex)
{
Page.Response.Write(ex.ToString());
Page.Response.Write("<script>alert('添加第" + count.ToString() + "个学生时数据出错,请检查数据!')</script>");
objConn.Close();//关闭EXCEL的连接
}
finally
{
objConn.Close();//关闭EXCEL的连接
conn.Close();
}
}
[解决办法]
objDataset1设置为公用的临时变量,而没必要去两次访问Excel,可能会造成文件在读取没有关闭,而另一个进程试图访问的情况
[解决办法]
OleDbConnection objConn = new OleDbConnection(sConnectionString);
objConn.Open(); 打开了,没有关闭,在执行的时候估计有问题。
或者点击第二个按钮的时候直接从GridView1 获得数据插入到数据库。