把DGV数据放到datatable DataTable dt = new DataTable(); for (int j = 0; j ucgrd.Columns.Count; j++) { dt.Columns.Add(ucgrd.Columns[j].HeaderCell.Value.ToString()); } for (int j = 0; j ucgrd.Rows.Count; j++) { DataRow dr = dt...
成都创新互联公司于2013年成立,先为浠水等服务建站,浠水等地企业,进行企业商务咨询服务。为浠水企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
以下是完整模块
Imports
System.Data
Imports
System.IO
Imports
System.Data.OleDb
Module
Module1
Public
cn
As
New
OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source="
Application.StartupPath
"\mdb数据库名字.mdb")
'定义连接
Public
DataBaseRST
As
Integer
'用来返回数据库执行结果
Public
Function
DataModify(ByVal
str
As
String)
As
Boolean
'进行数据库修改操作
Dim
cmdinsert
As
New
OleDbCommand
Try
cmdinsert.CommandText
=
str
cmdinsert.Connection
=
cn
If
cn.State
=
ConnectionState.Closed
Then
cn.Open()
DataBaseRST
=
cmdinsert.ExecuteNonQuery()
'用来返回执行的结果
cn.Close()
Return
True
Catch
ex
As
Exception
MessageBox.Show(Err.Description,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error)
Return
False
End
Try
End
Function
Public
Function
Search(ByVal
str
As
String,
ByVal
DGV
As
DataGridView)
As
Boolean
'查询
str---查询命令,DGV---DataGridView,用来显示数据的控件
Dim
tb
As
New
DataTable
Try
Dim
ap
As
New
OleDb.OleDbDataAdapter(str,
cn)
ap.Fill(tb)
DGV.DataSource
=
tb
Return
True
Catch
ex
As
Exception
MessageBox.Show(Err.Description,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error)
Return
False
End
Try
End
Function
End
Module
'以下是调用方法
DataModify("
insert
into
aa
values
('1','2')")'-------这里是数据库更新操作
Search("select
bb
from
aa",DataGridView1)'-----------这里是数据表查询操作
Try
gogoodsinfo.View(" where 商品编号='" + TextBox1.Text.replace("'","''") + "'")
Catch ex As Exception
messagebox.show("错误信息:"+ex.tostring(),"提示")
End Try
你试一下,把我这句给复制过去看是否报错,左边条件要有空格,右边要有取代函数!
select * from 表 where
(case when 条件 then 1 else 0 end+
case when 条件 then 1 else 0 end+
case when 条件 then 1 else 0 end+
case when 条件 then 1 else 0 end+
case when 条件 then 1 else 0 end) BETWEEN 2 and 5
dim
myselectquery
as
string
=
"select
*
from
表1
where
姓名='小强'"
dim
mycommand
as
new
sqlcommand
(myselectquery,
conn)
'建立一个command控件,conn是你的sqlconnection对象
conn.open()'打开数据连接
dim
myreader
as
sqldatareader'定义一个reader用来读数据
myreader
=
mycommand.executereader()'运行你的查询,结果到myreader
if
myreader.read()
then
'如果查到了数据
msgbox(myreader.getstring(0))
'显示第一个字段
end
if