Excel到SQL Server - C#(Excel to SQL Server - 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"); } }
DB上的表格:
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我改变了一点,但大多数想法都来自这些链接。
欢迎任何帮助! 谢谢。
Idea:
- Read a XLS and upload the data on a Microsoft SQL
Problem:
- Only the first column is being upload to the DB
My code:
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"); } }
Table on the DB:
CREATE TABLE [dbo].[Table1]( [student] [varchar](50) NULL, [rollno] [int] NULL, [course] [varchar](50) NULL ) ON [PRIMARY] GO
If you wanna access 2 images from examples of the XLS and DB: https://drive.google.com/drive/folders/0B98UpTa2n4XbeHZtOHU2cVotUms?usp=sharing
Finally, most part of this code I took from: https://code.msdn.microsoft.com/office/Import-Excel-Spreadsheet-2b7ca7cf#content I changed a little bit but most ideas come from these links.
Any help is welcome! Thank you.
原文:https://stackoverflow.com/questions/44590007
最满意答案
看看这个工作代码。 最后能够找出正确的方法来做到这一点:
在我的
render()
:<Mapbox.MapView ref={map => { this.map = map; }} styleURL={Mapbox.StyleURL.Basic} zoomLevel={15} centerCoordinate={[11.256, 43.770]} style={{flex: 1}} showUserLocation={true} userTrackingMode={Mapbox.UserTrackingModes.Follow}> {this.state.markers.map(marker => ( <Mapbox.PointAnnotation key= {marker.title} id= {marker.title} coordinate={marker.coordinates}> <View style={styles.annotationContainer}> <View style={styles.annotationFill} /> </View> <Mapbox.Callout title={marker.title} /> </Mapbox.PointAnnotation> ))} </Mapbox.MapView>
更新
this.state.markers
函数:_getAnnotations = (key, location) => { let newelement = { title: key, coordinates: location, }; this.setState(prevState => ({ markers: [...prevState.markers, newelement] })) }
地理查询
trigger
:this.state.geoQuery.on("key_entered", (key, location, distance) => { this._getAnnotations(key,location); });
Check out this working code. Finally was able to figure out the correct way to do it :
Inside my
render()
:<Mapbox.MapView ref={map => { this.map = map; }} styleURL={Mapbox.StyleURL.Basic} zoomLevel={15} centerCoordinate={[11.256, 43.770]} style={{flex: 1}} showUserLocation={true} userTrackingMode={Mapbox.UserTrackingModes.Follow}> {this.state.markers.map(marker => ( <Mapbox.PointAnnotation key= {marker.title} id= {marker.title} coordinate={marker.coordinates}> <View style={styles.annotationContainer}> <View style={styles.annotationFill} /> </View> <Mapbox.Callout title={marker.title} /> </Mapbox.PointAnnotation> ))} </Mapbox.MapView>
Function to update
this.state.markers
:_getAnnotations = (key, location) => { let newelement = { title: key, coordinates: location, }; this.setState(prevState => ({ markers: [...prevState.markers, newelement] })) }
Geoquery
trigger
:this.state.geoQuery.on("key_entered", (key, location, distance) => { this._getAnnotations(key,location); });
相关问答
更多-
在原生反应中使用Mapbox上的以下视图呈现自定义注释(Rendering custom annotation with following view on Mapbox in react native)[2024-02-21]
看看这个工作代码。 最后能够找出正确的方法来做到这一点: 在我的render() :{ this.map = map; }} styleURL={Mapbox.StyleURL.Basic} zoomLevel={15} centerCoordinate={[11.256, 43.770]} style={{flex: 1}} showUserLocation={true} userTracki ... -
在与Mapbox支持直接聊天后,他们告诉我PointAnnotation是遗留的,应该使用ShapeSource和SymbolLayer,它有更好的性能。 这是怎么做的:
this._map = c} ... -
只需遵循此示例并实现所需或委托方法 https://www.mapbox.com/mapbox-ios-sdk/examples/marker-custom-image/ Just follow this example and implement the needed or delegate methods https://www.mapbox.com/mapbox-ios-sdk/examples/marker-custom-image/
-
如何将自定义属性从Mapbox注释传递到其标注视图?(How do I pass in custom properties from Mapbox annotation to its callout view?)[2023-09-14]
我可能会遗漏一些东西,但您肯定只是检查是否可以将MGLAnnotation对象强制转换为MasterMapAnnotation: guard let masterAnnotation : MasterMapAnnotation = representedObject as? MasterMapAnnotation else { return } let tags : String? = masterAnnotation.tags // etc 显然,除了使用后卫之外,还有其他方法来控制流量,但我希望你 ... -
点击后如何在注释上居中或重新定位Mapbox地图?(How to center or reposition Mapbox map on annotation after being tapped?)[2022-02-09]
您可以通过在-mapView:didSelectAnnotation:中将地图视图的中心坐标设置为注释的坐标,使地图视图居中于注释上。 如果您正在使用注释视图,另一个选项是在-mapView中设置地图视图的中心坐标:didSelectAnnotationView : . 例如: - (void)mapView:(MGLMapView *)mapView didSelectAnnotation:(id)annotation { mapView.centerCoordinate = ... -
React Native Mapbox - 在Android上覆盖其他地图的地图(React Native Mapbox - Map overlaying other map on Android)[2024-01-04]
我在Mapbox上处理react native repo。 由于React View不是从MapView继承而且目前存在修复v5的android实现的问题,因此修复它是一项巨大的任务。 幸运的是,我们目前正在重写repo,我已经在我们的v6分支上解决了这个问题。 根据您需要的功能,您可能会开始使用v6分支。 您可以随时访问我们的gitter聊天室,我可以帮助您回答任何问题。 I work on the react native repo at Mapbox. There is currently an is ... -
Xcode中有任何错误吗? 此外,删除不必要的代码可能是一个好主意,只需尝试向地图添加注释即可。 如果您也可以尝试在react playground上运行代码,我可以帮助更好地调试这个,这是一个例子: https : //rnplay.org/apps/KR9WOw Are there any errors in Xcode? Also, it might be a good idea to remove unnecessary code from and just try and add annotati ...
-
Podspecs是指定如何集成pod及其元数据的文件。 您需要创建一个名为Podfile的文件,而不是Podspec 。 您还可以运行pod init以从基本模板创建一个空的。 Podspecs are files that specify how to integrate a pod and its metadata. You need to create a file named Podfile, not Podspec. You can also run pod init to create an e ...
-
看看这个在Mapbox Android演示应用程序中找到的例子 ,它可以帮助你。 示例中找到的重要代码片段: mapboxMap.setInfoWindowAdapter(new MapboxMap.InfoWindowAdapter() { @Nullable @Override public View getInfoWindow(@NonNull Marker marker) { // The info window layout is create ...
-
这段代码可能会为您解决问题: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ScrollView sv = (ScrollView) findViewById(R.id.scrollView); // Setup the MapVie ...