ConnectionString属性尚未初始化。我跑的时候显示错误 [英] The ConnectionString property has not been initialized. error shows when i run
本文介绍了ConnectionString属性尚未初始化。我跑的时候显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我准备了excel表格,并将数据读取到数据库中。
代码如下;
i prepared excel sheet and read that excel data in to the database.
code as follows;
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection("Server=(local);Initial Catalog=master;Integrated Security=True ");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUpload_Click(object sender, EventArgs e)
{
if ((txtFilePath.HasFile))
{
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
String query = null;
string connString = "";
string strFileName = DateTime.Now.ToString("ddMMyyyy_HHmmSS");
string strFileType = System.IO.Path.GetExtension(txtFilePath.FileName).ToString().ToLower();
string strNewPath = Server.MapPath("~UploadedExcel/" + strFileName + strFileType);
//Connection String to Excel Workbook
if (strFileType.Trim() == ".xls")
{
connString = "Server=(local);Initial Catalog=master;Integrated Security=True" + strNewPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Server=(local);Initial Catalog=master;Integrated Security=True" + strNewPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
query = "select * from StaffDetails";
if (conn.State == ConnectionState.Closed) conn.Open();
cmd = new SqlCommand(query, conn);
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
grvExcelData.DataSource = ds.Tables[0];
grvExcelData.DataBind();
lblMessage.Text = " data retrieved successfully! TOtal Records:" + ds.Tables[0].Rows.Count;
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Visible = true;
da.Dispose();
conn.Close();
conn.Dispose();
}
else
{
lblMessage.Text = "Please select an excel file first";
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Visible = true;
}
}
}
当我运行错误显示时;
ConnectionString属性尚未初始化。
conncetionstring line中的
什么是error.please帮助热线。
i认为以下行错误;
when i run error shows;
The ConnectionString property has not been initialized.
in the conncetionstring line what is the error.please help line.
i think in the following line error;
connString = "Server=(local);Initial Catalog=master;Integrated Security=True" + strNewPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
请帮助我。
please help me.
推荐答案
http://connectionstrings.com/excel [ ^ ]可能会帮助你。
http://connectionstrings.com/excel[^] might help you out.
尝试使用下面的连接字符串。
Provider = Microsoft.Jet.OLEDB.4.0;数据源= C:\ MyExcel.xls ;扩展属性=Excel 8.0; HDR =是; IMEX = 1;
Try to use this below connection string.
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties="Excel 8.0;HDR=Yes;IMEX=1";
这篇关于ConnectionString属性尚未初始化。我跑的时候显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文