c# - Excel 到 SQL Server - C#

标签 c#

想法:

  • 读取 XLS 并通过 Microsoft SQL 上传数据

问题:

  • 只有第一列正在上传到数据库

我的代码:

  private void button1_Click(object sender, EventArgs e) {
   // string path = @"XXXX\xls_test\Book1.xlsx";
   string path = @ "XXXX\xls_test\Book1.xlsx";
   ImportDataFromExcel(path);
  }


  public void ImportDataFromExcel(string excelFilePath) {
   //declare variables - edit these based on your particular situation 
   string ssqltable = "Table1";
   // make sure your sheet name is correct, here sheet name is sheet1, so you can change your sheet name if have    different 
   string myexceldataquery = "select student from [Sheet1$]";
   try {


    //create our connection strings 
    //string sexcelconnectionstring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFilePath +  ";" + "Extended Properties=" + "\"Excel 8.0; HDR=Yes; IMEX=1;\"";
    string sexcelconnectionstring = @ "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= XXXX\xls_test\Book1.xlsx" + ";" + "Extended Properties=Excel 12.0";

    string ssqlconnectionstring = "XXXX";
    //execute a query to erase any previous data from our destination table 
    string sclearsql = "delete from " + ssqltable;
    SqlConnection sqlconn = new SqlConnection(ssqlconnectionstring);
    SqlCommand sqlcmd = new SqlCommand(sclearsql, sqlconn);
    sqlconn.Open();
    sqlcmd.ExecuteNonQuery();
    sqlconn.Close();
    //series of commands to bulk copy data from the excel file into our sql table 
    OleDbConnection oledbconn = new OleDbConnection(sexcelconnectionstring);
    OleDbCommand oledbcmd = new OleDbCommand(myexceldataquery, oledbconn);

    oledbconn.Open();
    OleDbDataReader dr = oledbcmd.ExecuteReader();
    SqlBulkCopy bulkcopy = new SqlBulkCopy(ssqlconnectionstring);
    bulkcopy.DestinationTableName = ssqltable;
    while (dr.Read()) {
     bulkcopy.WriteToServer(dr);
    }
    dr.Close();
    oledbconn.Close();
    MessageBox.Show("File imported into sql server.");
   } catch (Exception ex) {
    //handle exception
    MessageBox.Show(ex.ToString());
    MessageBox.Show("Enter exception");
   }
  }

数据库上的表:

CREATE TABLE [dbo].[Table1](
    [student] [varchar](50) NULL,
    [rollno] [int] NULL,
    [course] [varchar](50) NULL
) ON [PRIMARY]
GO

如果您想从 XLS 和 DB 的示例中访问 2 个图像: https://drive.google.com/drive/folders/0B98UpTa2n4XbeHZtOHU2cVotUms?usp=sharing

最后,这段代码的大部分内容来自: https://code.msdn.microsoft.com/office/Import-Excel-Spreadsheet-2b7ca7cf#content 我做了一点改动,但大多数想法都来自这些链接。

欢迎任何帮助!谢谢。

最佳答案

有点猜测,但你有:

string myexceldataquery = "select student from [Sheet1$]";

然后你说

Only the first column is being upload to the DB

如果您仅在列上进行选择,这似乎完全符合预期。

也许也可以选择您想要的其他字段(我猜是它们!):

string myexceldataquery = "select student, rollno, course from [Sheet1$]";

关于c# - Excel 到 SQL Server - C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44590007/

相关文章:

c# - 如何在外部浏览器中打开 webBrowser 控件中的链接?

c# - 我可以在 C# 中创建一个接受两种不同类型的泛型方法吗

c# - 在 XAML 中设置元素内容时,WPF 如何确定要设置的属性?

c# - 向外部调用程序 C++ 返回一个值

c# - 从 Visual Studio 运行应用程序时性能下降

c# - Math.Pow 的最佳实践

c# - 实时与开发连接字符串

c# - 如何制作淡入/淡出弹出窗口?

c# - kernel32.dll 中的 LoadLibrary 函数在 Asp Web 应用程序中返回零

c# - 如何在不重置堆栈跟踪的情况下抛出异常?