如何将DataTable插入数据库? [英] How do you insert a DataTable into a Database?

查看:61
本文介绍了如何将DataTable插入数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我是这个整个.net场景的新手,所以我一直在努力寻找如何将新创建的表添加到数据库(连接)中的示例?

这里''我到目前为止!


//创建表

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 .net 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插入数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆