写入Access数据库 [英] Write to an Access database

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

问题描述

我有使用schema.ini文件读取和解析文本文件的代码。作品

太棒了。当我看到dataGrid时,它正是我想要的。


Dim CString As String = _

" Provider = Microsoft.Jet.OLEDB。 4.0;" &安培; _

数据来源= C:\; &安培; _

"扩展属性="" text; HDR = No;"""


Dim TConnect As New System.Data。 OleDb.OleDbConnection(CString)


TConnect.Open()


Dim da As New System.Data.OleDb.OleDbDataAdapter(" Select) *来自

837P.txt",TConnect)


Dim ds作为新数据集(Bananas)


da.Fill(ds)

DataGridView1.DataSource = ds.Tables(0)


我现在想从dataGridVew中获取此表并写入它是我的

Access数据库。


我有代码连接到数据库。我可以对它运行删除查询但是我不知道如何将这些数据添加到它中。我已经在互联网上了

我接近但没有快乐。


这里是我为Access半部分提供的代码:


Dim objConnection As New OleDbConnection(accessConnect)

''Dim strSQL As String =" Select * INTO z FROM" &安培;桌子和桌子""

Dim strSQL As String =" DELETE text_table。* FROM text_table;"

Dim objCommand As New OleDbCommand(strSQL,objConnection)

Dim objDataAdapter As New OleDbDataAdapter(objCommand)

Dim objDataTable As New Data.DataTable(" text_table")

Dim objDataRow As DataRow

昏暗的intRowsAffectedAffected整数


尝试

''打开数据库连接

objConnection.Open()

intRowsAffected = objCommand.ExecuteNonQuery()

Catch oledbexceptionerr As Exception

MessageBox.Show(oledbexceptionerr.Message)

结束尝试


objConnection.Close()


有没有办法调整这段代码让它写我的表到

" text_table"在我的数据库中?

I have code that reads and parses a text file using a schema.ini file. Works
great. When I see the dataGrid it''s exactly what I want.

Dim CString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\;" & _
"Extended Properties=""text;HDR=No;"""

Dim TConnect As New System.Data.OleDb.OleDbConnection(CString)

TConnect.Open()

Dim da As New System.Data.OleDb.OleDbDataAdapter("Select * from
837P.txt", TConnect)

Dim ds As New DataSet("Bananas")

da.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)

I now want to take this table from the dataGridVew and write it out to my
Access database.

I have code to connect to the db. I can run delete queries against it but I
can''t figure out how to add this data to it. I''ve been all over the internet
and I get close but no joy.

Here''s the code I have for the Access half:

Dim objConnection As New OleDbConnection(accessConnect)
''Dim strSQL As String = "Select * INTO z FROM " & table & ";"
Dim strSQL As String = "DELETE text_table.* FROM text_table;"
Dim objCommand As New OleDbCommand(strSQL, objConnection)
Dim objDataAdapter As New OleDbDataAdapter(objCommand)
Dim objDataTable As New Data.DataTable("text_table")
Dim objDataRow As DataRow
Dim intRowsAffected As Integer

Try
''open db connection
objConnection.Open()
intRowsAffected = objCommand.ExecuteNonQuery()

Catch oledbexceptionerr As Exception
MessageBox.Show(oledbexceptionerr.Message)
End Try

objConnection.Close()

Is there a way to just tweak this code to get it to write my table to
"text_table" in my db?

推荐答案

澄清一下:我认为我需要的是一种构建SQL的方法来

参考下面的ds.tables(0)类似于SELECT * INTO

text_table FROM dsTable哪里昏暗的dsTable dataTable = ds.tables(0),

但是我不能让它工作。


我在一篇有用的帖子上看到用于更新数据库的SQL就是所有需要的b $ b。哈!


吉姆


P.S.我在其他帖子中看到了问题,但我从未见过

直接回答。请不要发送给我关于这个主题的参考。

那是我过去8小时一直在看的东西但仍然不能

得到它。也许它只是我和它太晚了我太累了。 :-(哦

哦......自我怜悯开始蔓延。我离开这里。


Jim写道:
To clarify: what I think I need is a way to construct the SQL to
reference the ds.tables(0) below is something like "SELECT * INTO
text_table FROM dsTable" Where Dim dsTable dataTable = ds.tables(0),
but I can''t get that to work.

I saw on one helpful post that an SQL to update the db was all that''s
needed. HA!

Jim

P.S. I''ve seen the question asked in other posts but I''ve never seen a
direct answer. Please don''t send me off to a reference on the topic.
That''s what I''ve been looking at for the last 8 hours and still can''t
get it. Maybe its just me and its too late and I''m too tired. :-( oh
oh... self pity is starting to creep in. I''m out of here.

Jim wrote:

我有使用schema.ini文件读取和解析文本文件的代码。工作

很棒。当我看到dataGrid时,它是'正是我想要的。


Dim CString As String = _

" Provider = Microsoft.Jet.OLEDB.4.0;"& _

"数据源= C:\;"& _

"扩展属性="" text; HDR =否;"""


Dim TConnect As New System.Data.OleDb.OleDbConnection(CString)

TConnect.Open()

Dim da As New System.Data.OleDb.OleDbDataAdapter(" Select * from

837P.txt",TConnect)


Dim ds As New DataSet(Bananas)


da.Fill(ds)

DataGridView1.Data Source = ds.Tables(0)

我现在想从dataGridVew中获取该表并将其写入我的

Access数据库。


我有连接数据库的代码。我可以对它运行删除查询但是我不知道如何将这些数据添加到它中。我已经在互联网上了

我接近但没有快乐。


这里是我为Access半部分提供的代码:

Dim dsTable dataTable = ds.tables(0)

Dim objConnection As New OleDbConnection(accessConnect)

''Dim strSQL As String =" Select * INTO z FROM" &安培;桌子和桌子""

Dim strSQL As String =" DELETE text_table。* FROM text_table;"

Dim objCommand As New OleDbCommand(strSQL,objConnection)

Dim objDataAdapter As New OleDbDataAdapter(objCommand)

Dim objDataTable As New Data.DataTable(" text_table")

Dim objDataRow As DataRow

昏暗的intRowsAffectedAffected整数


尝试

''打开数据库连接

objConnection.Open()

intRowsAffected = objCommand.ExecuteNonQuery()

Catch oledbexceptionerr As Exception

MessageBox.Show(oledbexceptionerr.Message)

结束尝试


objConnection.Close()


有没有办法调整这段代码让它写我的表到

" text_table"在我的数据库中?

I have code that reads and parses a text file using a schema.ini file. Works
great. When I see the dataGrid it''s exactly what I want.

Dim CString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\;" & _
"Extended Properties=""text;HDR=No;"""

Dim TConnect As New System.Data.OleDb.OleDbConnection(CString)

TConnect.Open()

Dim da As New System.Data.OleDb.OleDbDataAdapter("Select * from
837P.txt", TConnect)

Dim ds As New DataSet("Bananas")

da.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)

I now want to take this table from the dataGridVew and write it out to my
Access database.

I have code to connect to the db. I can run delete queries against it but I
can''t figure out how to add this data to it. I''ve been all over the internet
and I get close but no joy.

Here''s the code I have for the Access half:
Dim dsTable dataTable = ds.tables(0)

Dim objConnection As New OleDbConnection(accessConnect)
''Dim strSQL As String = "Select * INTO z FROM " & table & ";"
Dim strSQL As String = "DELETE text_table.* FROM text_table;"
Dim objCommand As New OleDbCommand(strSQL, objConnection)
Dim objDataAdapter As New OleDbDataAdapter(objCommand)
Dim objDataTable As New Data.DataTable("text_table")
Dim objDataRow As DataRow
Dim intRowsAffected As Integer

Try
''open db connection
objConnection.Open()
intRowsAffected = objCommand.ExecuteNonQuery()

Catch oledbexceptionerr As Exception
MessageBox.Show(oledbexceptionerr.Message)
End Try

objConnection.Close()

Is there a way to just tweak this code to get it to write my table to
"text_table" in my db?


Jim,


在网络中,你主要使用绑定控件。 DataGridView是一个

复杂的数据绑定控件。


您可以使用强类型数据集(数据表)和非强类型

键入的,比如基本的。


在强类型数据集的区域中,我们每次都看到更多

进度。作为Net 1.x时代的例子,每个人只在这里谈论非强类型的,现在它变得越来越强烈打字




但是你怎么做也没有错,但你现在要建立自己的

更新,插入和删除SQL语句。


但是你也可以使用OleDBCommandbuilder一个不喜欢的命令

然而很多人在我的想法中是完美的,只要你的更新是简单的那么b $ b它们实际上来自网格。

http://msdn2.microsoft.com/en-us/lib...ndbuilder.aspx


我希望这有帮助,


Cor


" Jim" < Ji*@discussions.microsoft.comschreef in bericht

news:55 *************************** ******* @ microsof t.com ...
Jim,

In Net you are mostly working with binded controls. The DataGridView is a
complex databinded control.

You can work with strongly typed datasets (datatable) and with non strongly
typed ones, let say basic ones.

In the area of the strongly typed datasets we see every time much more
progress. As example in the time of Net 1.x everybody was talking here only
about non strongly typed ones, now it becomes more and more strongly typed
ones.

However nothing wrong how you did it, however you have now to build your own
Update, Insert and Delete SQL statemements.

But you can also use the OleDBCommandbuilder a command wich is disliked by
many people however is in my idea perfect as long as that your updates are
as simple that they come in fact from a grid.

http://msdn2.microsoft.com/en-us/lib...ndbuilder.aspx

I hope this helps,

Cor

"Jim" <Ji*@discussions.microsoft.comschreef in bericht
news:55**********************************@microsof t.com...

>我有使用schema.ini文件读取和解析文本文件的代码。
工程

太棒了。当我看到dataGrid时,它正是我想要的。


Dim CString As String = _

" Provider = Microsoft.Jet.OLEDB。 4.0;" &安培; _

数据来源= C:\; &安培; _

"扩展属性="" text; HDR = No;"""


Dim TConnect As New System.Data。 OleDb.OleDbConnection(CString)


TConnect.Open()


Dim da As New System.Data.OleDb.OleDbDataAdapter(" Select) *来自

837P.txt",TConnect)


Dim ds作为新数据集(Bananas)


da.Fill(ds)

DataGridView1.DataSource = ds.Tables(0)


我现在想从dataGridVew中获取此表并写入它是我的

Access数据库。


我有代码连接到数据库。我可以对它运行删除查询但是



无法弄清楚如何将这些数据添加到它。我已经到处了

互联网

我接近但没有快乐。


这是'我对Access半部分的代码:


Dim objConnection As New OleDbConnection(accessConnect)

''Dim strSQL As String =" Select * INTO z FROM " &安培;桌子和桌子""

Dim strSQL As String =" DELETE text_table。* FROM text_table;"

Dim objCommand As New OleDbCommand(strSQL,objConnection)

Dim objDataAdapter As New OleDbDataAdapter(objCommand)

Dim objDataTable As New Data.DataTable(" text_table")

Dim objDataRow As DataRow

昏暗的intRowsAffectedAffected整数


尝试

''打开数据库连接

objConnection.Open()

intRowsAffected = objCommand.ExecuteNonQuery()

Catch oledbexceptionerr As Exception

MessageBox.Show(oledbexceptionerr.Message)

结束尝试


objConnection.Close()


有没有办法调整这段代码让它写我的表到

" text_table"在我的数据库中?

>I have code that reads and parses a text file using a schema.ini file.
Works
great. When I see the dataGrid it''s exactly what I want.

Dim CString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\;" & _
"Extended Properties=""text;HDR=No;"""

Dim TConnect As New System.Data.OleDb.OleDbConnection(CString)

TConnect.Open()

Dim da As New System.Data.OleDb.OleDbDataAdapter("Select * from
837P.txt", TConnect)

Dim ds As New DataSet("Bananas")

da.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)

I now want to take this table from the dataGridVew and write it out to my
Access database.

I have code to connect to the db. I can run delete queries against it but
I
can''t figure out how to add this data to it. I''ve been all over the
internet
and I get close but no joy.

Here''s the code I have for the Access half:

Dim objConnection As New OleDbConnection(accessConnect)
''Dim strSQL As String = "Select * INTO z FROM " & table & ";"
Dim strSQL As String = "DELETE text_table.* FROM text_table;"
Dim objCommand As New OleDbCommand(strSQL, objConnection)
Dim objDataAdapter As New OleDbDataAdapter(objCommand)
Dim objDataTable As New Data.DataTable("text_table")
Dim objDataRow As DataRow
Dim intRowsAffected As Integer

Try
''open db connection
objConnection.Open()
intRowsAffected = objCommand.ExecuteNonQuery()

Catch oledbexceptionerr As Exception
MessageBox.Show(oledbexceptionerr.Message)
End Try

objConnection.Close()

Is there a way to just tweak this code to get it to write my table to
"text_table" in my db?



Cor,


感谢您的见解。我看了你的参考资料,我仍然有点困惑。我是.net的新手。我读过其他帖子

据说只是构建SQL语句。我看不出怎么做

我也不知道OleDBCommandbuilder会如何帮助你做到这一点。我知道我在这里错过了一些简单的东西。


有人能给我一个简短的例子说明SQL语句可能是什么吗? $ b看起来像是基于我下面的代码?


Jim


Cor Ligthert [MVP]写道:
Cor,

Thanks for the insight. I''ve looked at your reference and I''m still
somewhat confused. I''m a newbie in .net. I''ve read other posts where
it was said "just build the SQL statements". I can''t see how to do that
nor can I see how the OleDBCommandbuilder will help do that. I know I''m
missing something simple here.

Can someone give me a short example of what that SQL statement might
look like based on my code below?

Jim

Cor Ligthert [MVP] wrote:

Jim,


在Net中,你主要使用绑定控件。 DataGridView是一个

复杂的数据绑定控件。


您可以使用强类型数据集(数据表)和非强类型

键入的,比如基本的。


在强类型数据集的区域中,我们每次都看到更多

进度。作为Net 1.x时代的例子,每个人只在这里谈论非强类型的,现在它变得越来越强烈打字




但是你怎么做也没有错,但你现在要建立自己的

更新,插入和删除SQL语句。


但是你也可以使用OleDBCommandbuilder一个不喜欢的命令

然而很多人在我的想法中是完美的,只要你的更新是简单的那么b $ b它们实际上来自网格。

http://msdn2.microsoft.com/en-us/lib...ndbuilder.aspx


我希望这有帮助,


Cor


" Jim" < Ji*@discussions.microsoft.comschreef in bericht

news:55 *************************** ******* @ microsof t.com ...
Jim,

In Net you are mostly working with binded controls. The DataGridView is a
complex databinded control.

You can work with strongly typed datasets (datatable) and with non strongly
typed ones, let say basic ones.

In the area of the strongly typed datasets we see every time much more
progress. As example in the time of Net 1.x everybody was talking here only
about non strongly typed ones, now it becomes more and more strongly typed
ones.

However nothing wrong how you did it, however you have now to build your own
Update, Insert and Delete SQL statemements.

But you can also use the OleDBCommandbuilder a command wich is disliked by
many people however is in my idea perfect as long as that your updates are
as simple that they come in fact from a grid.

http://msdn2.microsoft.com/en-us/lib...ndbuilder.aspx

I hope this helps,

Cor

"Jim" <Ji*@discussions.microsoft.comschreef in bericht
news:55**********************************@microsof t.com...

>我有使用schema.ini文件读取和解析文本文件的代码。
工作好极了。当我看到dataGrid时,它正是我想要的。

Dim CString As String = _
" Provider = Microsoft.Jet.OLEDB.4.0;" &安培; _
数据来源= C:\; &安培; _
"扩展属性="" text; HDR = No;"""

Dim TConnect As New System.Data.OleDb.OleDbConnection(CString)

TConnect.Open()

Dim da As New System.Data.OleDb.OleDbDataAdapter(" Select * from
837P.txt",TConnect)
Dim ds As New DataSet(Bananas)

da.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
Access数据库。

我有代码连接到db。我可以对它运行删除查询但是
我不知道如何将这些数据添加到它。我已经遍布
互联网
我接近但没有快乐。

这是我为Access半部分的代码:

Dim objConnection As New OleDbConnection(accessConnect)
''Dim strSQL As String =" Select * INTO z FROM" &安培;桌子和桌子&;;"
Dim strSQL As String =" DELETE text_table。* FROM text_table;"
Dim objCommand As New OleDbCommand(strSQL,objConnection)
Dim objDataAdapter As New OleDbDataAdapter(objCommand )
Dim objDataTable As New Data.DataTable(" text_table")
Dim objDataRow As DataRow
Dim intRowsAffectedAffected As Integer

尝试
''打开数据库连接
objConnection.Open()
intRowsAffected = objCommand.ExecuteNonQuery()
将oledbexceptionerr作为异常捕获消息框.Show(oledbexceptionerr.Message)
结束尝试

objConnection.Close()

有没有办法只是调整这段代码,让它把我的表写到
" text_table"在我的数据库中?

>I have code that reads and parses a text file using a schema.ini file.
Works
great. When I see the dataGrid it''s exactly what I want.

Dim CString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\;" & _
"Extended Properties=""text;HDR=No;"""

Dim TConnect As New System.Data.OleDb.OleDbConnection(CString)

TConnect.Open()

Dim da As New System.Data.OleDb.OleDbDataAdapter("Select * from
837P.txt", TConnect)

Dim ds As New DataSet("Bananas")

da.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)

I now want to take this table from the dataGridVew and write it out to my
Access database.

I have code to connect to the db. I can run delete queries against it but
I
can''t figure out how to add this data to it. I''ve been all over the
internet
and I get close but no joy.

Here''s the code I have for the Access half:

Dim objConnection As New OleDbConnection(accessConnect)
''Dim strSQL As String = "Select * INTO z FROM " & table & ";"
Dim strSQL As String = "DELETE text_table.* FROM text_table;"
Dim objCommand As New OleDbCommand(strSQL, objConnection)
Dim objDataAdapter As New OleDbDataAdapter(objCommand)
Dim objDataTable As New Data.DataTable("text_table")
Dim objDataRow As DataRow
Dim intRowsAffected As Integer

Try
''open db connection
objConnection.Open()
intRowsAffected = objCommand.ExecuteNonQuery()

Catch oledbexceptionerr As Exception
MessageBox.Show(oledbexceptionerr.Message)
End Try

objConnection.Close()

Is there a way to just tweak this code to get it to write my table to
"text_table" in my db?



这篇关于写入Access数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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