本人是新手,以下代码不是很明白,所以还望大家多多指教!
代码有错!
protected void btnUpload_Click(object sender, EventArgs e)
{
string filename = FileUpload1.PostedFile.FileName;
/*string virtualName = "Temp/" + filename;
string localFilename = Server.MapPath(virtualName);
this.FileUpload1.SaveAs(localFilename);*/
this.Label1.Text = filename;
string mystring = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '" + filename + "';Extended Properties=Excel 8.0";
OleDbConnection cnnxls = new OleDbConnection(mystring);
OleDbDataAdapter myDa = new OleDbDataAdapter("select * from [Sheet1$]", cnnxls);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
cnnxls.Close();
if (myDs.Tables[0].Rows.Count > 0)
{
string strSql ="";
string CnnString = "Provider=PC-201103261848;Data Source=jieguo;Initial Catalog=pingjiaoMS;User Id=sa;Password=123";
OleDbConnection conn = new OleDbConnection(CnnString);
conn.Open();
OleDbCommand myCmd = null;
for (int i = 0; i < myDs.Tables[0].Rows.Count; i++)
{
strSql = "insert into Web_Processing (SourceID,Operator,SourceName) values ('";
strSql += myDs.Tables[0].Rows[i].ItemArray[0].ToString() + "', '";
strSql += myDs.Tables[0].Rows[i].ItemArray[2].ToString() + "', '";
strSql += myDs.Tables[0].Rows[i].ItemArray[3].ToString() + "')";
this.Label1.Text = strSql;
try
{
myCmd = new OleDbCommand(strSql, conn);
myCmd.ExecuteNonQuery();
Label1.Text = "<script language=javascript>alert('数据导入成功.');</script>";
}
catch
{
Label1.Text = "<script language=javascript>alert('数据导入失败.');</script>";
}
}
conn.Close();
}}
代码有错!
protected void btnUpload_Click(object sender, EventArgs e)
{
string filename = FileUpload1.PostedFile.FileName;
/*string virtualName = "Temp/" + filename;
string localFilename = Server.MapPath(virtualName);
this.FileUpload1.SaveAs(localFilename);*/
this.Label1.Text = filename;
string mystring = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '" + filename + "';Extended Properties=Excel 8.0";
OleDbConnection cnnxls = new OleDbConnection(mystring);
OleDbDataAdapter myDa = new OleDbDataAdapter("select * from [Sheet1$]", cnnxls);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
cnnxls.Close();
if (myDs.Tables[0].Rows.Count > 0)
{
string strSql ="";
string CnnString = "Provider=PC-201103261848;Data Source=jieguo;Initial Catalog=pingjiaoMS;User Id=sa;Password=123";
OleDbConnection conn = new OleDbConnection(CnnString);
conn.Open();
OleDbCommand myCmd = null;
for (int i = 0; i < myDs.Tables[0].Rows.Count; i++)
{
strSql = "insert into Web_Processing (SourceID,Operator,SourceName) values ('";
strSql += myDs.Tables[0].Rows[i].ItemArray[0].ToString() + "', '";
strSql += myDs.Tables[0].Rows[i].ItemArray[2].ToString() + "', '";
strSql += myDs.Tables[0].Rows[i].ItemArray[3].ToString() + "')";
this.Label1.Text = strSql;
try
{
myCmd = new OleDbCommand(strSql, conn);
myCmd.ExecuteNonQuery();
Label1.Text = "<script language=javascript>alert('数据导入成功.');</script>";
}
catch
{
Label1.Text = "<script language=javascript>alert('数据导入失败.');</script>";
}
}
conn.Close();
}}
解决方案 »
- 将英文单词分解成字母组合,求解!
- DataTable怎么实现类似html中ColSpan和RowSpan的几列合并或几行合并显示的结构
- webBrowser将word显示后,如何在winform中控制其中的word文档?
- 如何用C#编写图片查看器?大概的描述!
- 求 利用serialport 实现短信发送接受的Example
- 2000操作系统上的有关excel的拒绝访问错误
- 怎么传递事件??
- Byte[] PacketLength = System.BitConverter.GetBytes(int32 a);怎么得到标准字节数组
- 求解决 C# Form窗口切换出现现象?
- 一个关于.net事件处理的程序,能运行,结果不对,帮忙看一下
- 如何使数组中的元素进行降序排序
- 如何让 DataGridView单元格赋值 和 手动输入 效果相同 ?
先读取excel 放到一个table 再遍历table 生成sql语句 导入数据库 string mystring = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '" + filename + "';Extended Properties=Excel 8.0";
这是03的 "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties=\"Excel 12.0;HDR=YES\""
这个是07的