Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何生成excel的第一列并在数据库mssql中获取匹配数据。C语言_C# - Fatal编程技术网

C# 如何生成excel的第一列并在数据库mssql中获取匹配数据。C语言

C# 如何生成excel的第一列并在数据库mssql中获取匹配数据。C语言,c#,C#,我想获取excel的第一列并在数据库中自动生成,将其粘贴到c#的数据网格视图中。如果可能请添加一些代码。 public bool CheckExcel(string tableName, string localFileNamePath) { bool status=false; string Excel07ConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + localFileNamePath + ";Ex

我想获取excel的第一列并在数据库中自动生成,将其粘贴到c#的数据网格视图中。如果可能

请添加一些代码。
public bool CheckExcel(string tableName, string localFileNamePath)
{
    bool status=false;
    string Excel07ConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + localFileNamePath + ";Extended Properties='Excel 12.0;IMEX=1'";
    try
    {
        var adapter = new OleDbDataAdapter("Select * FROM [Sheet1$]", Excel07ConString);
        var ds = new DataSet();
        adapter.Fill(ds, "myTable");
        DataTable data = ds.Tables["myTable"];
        //Data table hold only first column of excel file.
        DataTable CustomDT=new DataTable();
        CustomDT.Columns.Add("FirstColumnName", typeof(string));
        foreach(DataRow row in data.Rows)
        {
            CustomDT.Rows.Add(row("Columnn name in excel").ToString());
        }
        DataGridView.DateSource=CustomDt;
        //Function to write data in database.
        string sqlConnection = "Data Source=Database path";
        SqlCeConnection conn = new SqlCeConnection(sqlConnection);
        conn.Open();
        foreach(DataRow row in data.Rows)
        {
            string qryUser = "Insert into TableName(Column) values('" + row["ColumnName"].ToString() + "')";
            var comCheck = new SqlCeCommand(qryUser, conn);
            comCheck.ExecuteScalar();
        }
        comCheck.Dispose();
        conn.Close();
    }
}