C# Excel到sqlserver-C

C# Excel到sqlserver-C,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); } pub

想法:

读取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个图像:

最后,我从以下代码中提取了大部分代码: 我改变了一点,但大多数想法来自这些链接


欢迎任何帮助!多谢各位

有点猜测,但你有:

string myexceldataquery = "select student from [Sheet1$]";
然后你说

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

若您只选择on列,那个么这似乎是完全可以预料到的

也可以选择你想要的其他字段我猜是吧!:

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

有点猜测,但你有:

string myexceldataquery = "select student from [Sheet1$]";
然后你说

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

若您只选择on列,那个么这似乎是完全可以预料到的

也可以选择你想要的其他字段我猜是吧!:

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

如果您在excel文件中执行另存为并将其转换为.csv文件,则会更轻松。然后可以使用TextFieldParser并将分隔符设置为逗号

using (TextFieldParser parser = new TextFieldParser(csvFilePath))
{
    Dictionary<string, CustomRecord> csvDictionary = new Dictionary<string, CustomRecord>();

    parser.TextFieldType = FieldType.Delimited;
    parser.SetDelimiters(",");
    while (!parser.EndOfData)
    {
        //write to custom record
    }
稍后只需将CustomRecord写入数据库

foreach (KeyValuePair<string, CustomRecord> kvp in CustomRecord)
{
    //sql insert statement here using the key values from the dictionary as your sql values)
}

如果您在excel文件中执行另存为并将其转换为.csv文件,则会更轻松。然后可以使用TextFieldParser并将分隔符设置为逗号

using (TextFieldParser parser = new TextFieldParser(csvFilePath))
{
    Dictionary<string, CustomRecord> csvDictionary = new Dictionary<string, CustomRecord>();

    parser.TextFieldType = FieldType.Delimited;
    parser.SetDelimiters(",");
    while (!parser.EndOfData)
    {
        //write to custom record
    }
稍后只需将CustomRecord写入数据库

foreach (KeyValuePair<string, CustomRecord> kvp in CustomRecord)
{
    //sql insert statement here using the key values from the dictionary as your sql values)
}

哇,那是我演的幼稚角色!是的,问题出在你的猜测上。非常感谢。哇,那是我演的幼稚角色!是的,问题出在你的猜测上。非常感谢。