如何将DataTable插入数据库?

编程入门 行业动态 更新时间:2024-10-21 19:40:50
本文介绍了如何将DataTable插入数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

好吧,我是这个整个场景的新手,所以我一直在努力寻找如何将新创建的表添加到数据库(连接)中的示例? 这里''我到目前为止! //创建表 DataTable table = new DataTable(" MyTable"); table.Columns.Add(new DataColumn(" Name",Type.GetType(" System.String"))); table.Columns.Add(new DataColumn( " Age",Type.GetType(" System.Byte"))); //添加一行数据 DataRow dataRow = table .NewRow(); dataRow [" Name"] =" John"; dataRow [" Age"] = 30; table.Rows.Add(dataRow); // ???????????????????????? 。 。 (这里是我被困的地方) 如何添加我上面的表到下面的数据库连接? 。 。 // ????????????? ??????????? //创建数据库连接 OleDbConnection dbConn = new OleDbConnection(" Provider = Microsoft.Jet.OLEDB.4.0;数据来源= + DBFULLNAME); ---------------------------------- -------------------- 对不起,我知道他们必须在某个地方做个例子,但一直在寻找几个小时没有运气! 感谢您的帮助。

ok i''m new to this whole scene so i have been battling trying to find examples on how to add a newly created table into the database (connection)? Here''s what i have so far! //create table DataTable table = new DataTable("MyTable"); table.Columns.Add(new DataColumn("Name", Type.GetType("System.String"))); table.Columns.Add(new DataColumn("Age", Type.GetType("System.Byte"))); //add a row of data DataRow dataRow = table.NewRow(); dataRow["Name"] = "John"; dataRow["Age"] = 30; table.Rows.Add(dataRow); //????????????????????????? . . (Here''s where i''m stuck) How do I add my table above to the db connection below? . . //????????????????????????? //create database connection OleDbConnection dbConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + DBFULLNAME); ------------------------------------------------------ sorry i know their must be an example somewhere but have been searching for hours with no luck at all! thanks for any help.

推荐答案

如果您问如何获取数据在DataTable中保存的SQL Server的答案是将适配器或命令与DataTable和Update相关联。 如果您问如何将物理表添加到SQL Server数据库中会使用SMO \SQLDMO或直接TSQL,而不是ADO。您也可以使用OleDb连接来定义新表,但它不会包含任何数据。 If you are asking how to get data into SQL Server that is held in a DataTable the answer would be associate an Adapter or Command to DataTable and Update. If you are asking how to add a physical Table to a SQL Server Database you would use SMO\SQLDMO or straight TSQL, not ADO. You could also use an OleDb connection to define a new table but it would not have any data in it.

如果您问如何要将数据导入DataTable中保存的SQL Server,答案就是将适配器或命令与DataTable和Update相关联。 如果您询问如何将物理表添加到SQL Server数据库您将使用SMO \SQLDMO或直接TSQL,而不是ADO。您还可以使用OleDb连接来定义新表,但它不会包含任何数据。 If you are asking how to get data into SQL Server that is held in a DataTable the answer would be associate an Adapter or Command to DataTable and Update. If you are asking how to add a physical Table to a SQL Server Database you would use SMO\SQLDMO or straight TSQL, not ADO. You could also use an OleDb connection to define a new table but it would not have any data in it.

如果我上面的例子没有告诉你我被困在哪里,那么我不知所措? 我之后的所有内容都是我的DataSet和DBConnection之间缺少的链接!我已经创建了数据库,我只需要将该死的表插入其中。 我查看了DataAdapter,这将允许我提供DataTable我的数据集m使用contains,但是仍然没有让我更接近插入表格吗? 任何帮助都将不胜感激。

Well if my above example didn''t show you where i''m stuck, then i''m at a loss? All i''m after is the missing link between my DataSet and DBConnection! I''ve already created the database i just need to insert the damn table into it. I looked into the DataAdapter which will allow me to supply a DataSet that the DataTable i''m using contains, but that still doesn''t bring me any closer to inserting the table? any help would be appreciated.

ok看来没有人理解我的问题?我将尝试进一步解释。 1)我在项目文件夹中创建了一个新的空数据库,使用SQLConfigDataSource(win32 api) 2)现在我想在我的第一篇文章中使用上面的例子插入一个新表,该帖子使用DataSet,DataTable,DataColumn和DataColumn。和DataRow 我可以使用以下代码实现这一点:(但我更愿意学习如何使用上面的代码) --------------------- OleDbConnection dbConn = new OleDbConnection(" Provider = Microsoft.Jet .OLEDB.4.0;数据源= c:\mydb.mdb"); dbConn.Open(); OleDbCommand cmd = new OleDbCommand( ); cmd.Connection = dbConn; //创建表 cmd.CommandText =" CREATE TABLE mytable(名称VARCHAR(25),Age INTEGER)" ;; cmd.ExecuteNonQuery(); //添加一行数据 cmd.CommandText =" INSERT INTO mytable VALUES(''John'',30)" ;; cmd.ExecuteNonQuery(); dbConn.Close(); --------------------- 但是什么如果我不能插入最终的表格,那就是使用所有以前的课程进入数据库???? 我确定有一种方式,它可能只是几行代码但我只是没有找到合适的班级来做这件事,而且我厌倦了谷歌!如果有人可以提供帮助我真的会对它有所帮助。 ok it appears no one understands my problem? i''ll try to explain further. 1) i created a new empty database in my project folder, using SQLConfigDataSource (win32 api) 2) now i''d like to insert a new table using my example above in my first post, which uses "DataSet", "DataTable", "DataColumn" and "DataRow" I can achieve that using the following code: (but i''d much rather learn how to use the above code) --------------------- OleDbConnection dbConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\mydb.mdb"); dbConn.Open(); OleDbCommand cmd = new OleDbCommand(); cmd.Connection = dbConn; // create table cmd.CommandText = "CREATE TABLE mytable (Name VARCHAR(25), Age INTEGER)"; cmd.ExecuteNonQuery(); //add a row of data cmd.CommandText = "INSERT INTO mytable VALUES (''John'', 30)"; cmd.ExecuteNonQuery(); dbConn.Close(); --------------------- but what''s the use of having all those previous classes if i can''t insert the final table into the database???? i''m sure there''s a way and it''s probably just a couple of lines of code but i just haven''t found the right class to do it, and i''m sick of google!, if anyone could help i would really apreciate it.

更多推荐

如何将DataTable插入数据库?

本文发布于:2023-10-15 02:58:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1493038.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   数据库   DataTable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!