我做了一个EXCEL导入,在我本地测试的时没没事,成功写到表中.传到服务器上,在服务器上测试也不报错.
做了一个IIS后,在外网输入IP地址后报错!
代码如下: protected void Button1_Click1(object sender, EventArgs e)
{
string filePath = "";
conn = new SqlConnection(ConfigurationManager.AppSettings["YxConnection"].ToString().Trim());
if (FJ.PostedFile.FileName == "")
{
Response.Write("<script language=javascript>alert('请选择要上传的文件!');</script>");
return;
}
else
{
filePath = FJ.PostedFile.FileName;//取得文件路径
string sql_excel = "select mobile from OPENROWSET('MICROSOFT.JET.OLEDB.4.0','Excel 8.0;HDR=YES;DATABASE=" + filePath + "',sheet1$)";
cmd = new SqlCommand(sql_excel, conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
adapter.Fill(ds, "result");
dt = ds.Tables["result"];
GridView1.DataSource = dt;
GridView1.DataBind();
if (ds.Tables["result"].Rows.Count != 0)
{
string sql = ""; SqlConnection myConnection = new SqlConnection();
string strConn = System.Configuration.ConfigurationManager.AppSettings["YxConnection"].ToString();
myConnection.ConnectionString = strConn;
SqlCommand da = new SqlCommand();
try
{
for (int i = 0; i < ds.Tables["result"].Rows.Count; i++)
{
sql = "insert into table1(mobile) values ('" + ds.Tables["result"].Rows[i]["mobile"].ToString() + "')";
da = new SqlCommand(sql, myConnection);
if (da.Connection.State.ToString() == "Closed")
{
da.Connection.Open();
}
da.ExecuteNonQuery();
}
}
catch (Exception ex)
{
getErrMsg = ex.Message.ToString();
Response.Write(ex.Message.ToString());
}
finally
{
da.Connection.Close();
da = null;
}
if (getErrMsg == "" || getErrMsg == null)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script language='Javascript'>alert('导入成功!')</script>");
return;
}
else
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script language='Javascript'>alert('导入失败!')</script>");
return;
}
}
}
}
做了一个IIS后,在外网输入IP地址后报错!
代码如下: protected void Button1_Click1(object sender, EventArgs e)
{
string filePath = "";
conn = new SqlConnection(ConfigurationManager.AppSettings["YxConnection"].ToString().Trim());
if (FJ.PostedFile.FileName == "")
{
Response.Write("<script language=javascript>alert('请选择要上传的文件!');</script>");
return;
}
else
{
filePath = FJ.PostedFile.FileName;//取得文件路径
string sql_excel = "select mobile from OPENROWSET('MICROSOFT.JET.OLEDB.4.0','Excel 8.0;HDR=YES;DATABASE=" + filePath + "',sheet1$)";
cmd = new SqlCommand(sql_excel, conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
adapter.Fill(ds, "result");
dt = ds.Tables["result"];
GridView1.DataSource = dt;
GridView1.DataBind();
if (ds.Tables["result"].Rows.Count != 0)
{
string sql = ""; SqlConnection myConnection = new SqlConnection();
string strConn = System.Configuration.ConfigurationManager.AppSettings["YxConnection"].ToString();
myConnection.ConnectionString = strConn;
SqlCommand da = new SqlCommand();
try
{
for (int i = 0; i < ds.Tables["result"].Rows.Count; i++)
{
sql = "insert into table1(mobile) values ('" + ds.Tables["result"].Rows[i]["mobile"].ToString() + "')";
da = new SqlCommand(sql, myConnection);
if (da.Connection.State.ToString() == "Closed")
{
da.Connection.Open();
}
da.ExecuteNonQuery();
}
}
catch (Exception ex)
{
getErrMsg = ex.Message.ToString();
Response.Write(ex.Message.ToString());
}
finally
{
da.Connection.Close();
da = null;
}
if (getErrMsg == "" || getErrMsg == null)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script language='Javascript'>alert('导入成功!')</script>");
return;
}
else
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script language='Javascript'>alert('导入失败!')</script>");
return;
}
}
}
}
简单 理解,你只能导入 服务端,没办法 从1客服端导入到另一客服端!
这需要 数据传输 socket 的