SQL Server连接错误:实例失败

时间:2012-05-20 16:32:20

标签: sql

我正在尝试将excel文件中的数据插入到sql数据库列中。

protected void Button1_Click(object sender, EventArgs e)
    {
        using (OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["ExcelCon"].ConnectionString))
        {
            con.Open();
            OleDbCommand com = new OleDbCommand("Select * from [Sheet1$]", con);
            OleDbDataReader dr = com.ExecuteReader();
            using (SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Sql"].ConnectionString))
            {
                sqlcon.Open();
                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlcon))
                {
                    bulkCopy.ColumnMappings.Add("ExcelColumnHeaderName", "SqlColumnName");
                    bulkCopy.DestinationTableName = "tableName";
                    bulkCopy.WriteToServer(dr);
                }
            }
            dr.Close();
            dr.Dispose();
        }
        Response.Write("Upload Successfull!");
    }

网络配置:

   <connectionStrings>
  <add name="ExcelCon" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\John\\Desktop\\ExcelFile.xlsx;Extended Properties=Excel 12.0"/>
  <add name="Sql" connectionString="Data Source=.\\SQLEXPRESS;AttachDbFilename='c:\\users\\John\\documents\\visual studio 2010\\Projects\\WebApplication3\\WebApplication3\\App_Data\\Database1.mdf';Integrated Security=True;User Instance=True;"/>
  <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
        providerName="System.Data.SqlClient" />
</connectionStrings>

问题是我收到了这个错误,我不知道原因:

Exception Details: System.InvalidOperationException: Instance failure.
here:sqlcon.Open();

1 个答案:

答案 0 :(得分:11)

从连接字符串中删除双斜杠。