Imports System.Data
Imports System.IO
Imports System.Data.OleDb
Module Module1
Public cn1 As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0Data Source=C:\ACCESS数据库1.mdb") '定义连接1---这里请更改为实际数据库路径及名称
Public cn2 As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0Data Source=C:\ACCESS数据库2.mdb") '定义连接2---这里请更改为实际数据库路径及名称
Public DataBaseRST1 As Integer '用来返回数据库执行结果
Public DataBaseRST2 As Integer '用来返回数据库执行结果
Public Function DataModify(ByVal str_cmd1 As String, ByVal str_cmd2 As String) As Boolean '进行数据库修改 *** 作函数
Dim cmdinsert1 As New OleDbCommand
Dim cmdinsert2 As New OleDbCommand
Try
cmdinsert1.CommandText = str_cmd1
cmdinsert2.CommandText = str_cmd2
cmdinsert1.Connection = cn1
cmdinsert2.Connection = cn2
If cn1.State = ConnectionState.Closed Then cn1.Open()
If cn2.State = ConnectionState.Closed Then cn2.Open()
DataBaseRST1 = cmdinsert1.ExecuteNonQuery() '用来返回执行的结果
DataBaseRST2 = cmdinsert2.ExecuteNonQuery() '用来返回执行的结果
cn1.Close()
cn2.Close()
Return True
Catch ex As Exception
MessageBox.Show(Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
End Module
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DataModify("insert into aa values ('1','2')", "insert into aa values ('1','2')") '调用方法
End Sub
End Class
追问
这个方法我只能用于添加修改删除,请问,查询数据并且绑定到DGV怎么做?
回答
Public Function Search(ByVal cn1 As OleDb.OleDbConnection, ByVal cn2 As OleDb.OleDbConnection, ByVal str_cmd1 As String, ByVal str_cmd2 As String, ByVal DGV1 As DataGridView, ByVal DGV2 As DataGridView) As Boolean '查询 str_cmd1,str_cmd2---查询命令,DGV1,DGV2---DataGridView,用来显示数据的控件
Dim tb1 As New DataTable
Dim tb2 As New DataTable
Try
Dim ap1 As New OleDb.OleDbDataAdapter(str_cmd1, cn1)
ap1.Fill(tb1)
DGV1.DataSource = tb1
Dim ap2 As New OleDb.OleDbDataAdapter(str_cmd2, cn2)
ap2.Fill(tb2)
DGV2.DataSource = tb2
Return True
Catch ex As Exception
MessageBox.Show(Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
程序代码:using System.Data
using System.Data.OleDb
string strConnection="Provider=Microsoft.Jet.OleDb.4.0"
strConnection+=@"DataSource=C:\BegASPNET\Northwind.mdb"
OleDbConnection objConnection=new OleDbConnection(strConnection)
objConnection.Open()
objConnection.Close()
解释:
连接Access数据库需要导入额外的命名空间,所以有了最前面的两条using命令,这是必不可少的!
strConnection这个变量里存放的是连接数据库所需要的连接字符串,他指定了要使用的数据提供者和要使用的数据源。
“Provider=Microsoft.Jet.OleDb.4.0”是指数据提供者,这里使用的是Microsoft Jet引擎,也就是Access中的数据引擎,asp.net就是靠这个和Access的数据库连接的。
“Data Source=C:\BegASPNET\Northwind.mdb”是指明数据源的位置,他的标准形式是“Data Source=MyDrive:MyPath\MyFile.MDB”。
PS:
1.“+=”后面的“@”符号是防止将后面字符串中的“\”解析为转义字符。
2.如果要连接的数据库文件和当前文件在同一个目录下,还可以使用如下的方法连接:
strConnection+="Data Source="
strConnection+=MapPath("Northwind.mdb")
3.要注意连接字符串中的参数之间要用分号来分隔。
“OleDbConnection objConnection=new OleDbConnection(strConnection)”这一句是利用定义好的连接字符串来建立了一个链接对象,以后对数据库的 *** 作我们都要和这个对象打交道。
“objConnection.Open()”这用来打开连接。至此,与Access数据库的连接完成。
整删改查 都是sql语句 不分Winform和 Web下面的是你需要的 我的表命叫 story
#region Method
/// <summary>
/// 增加一条数据
/// </summary>
public int Add(BW.Model.story model)
{
StringBuilder strSql=new StringBuilder()
strSql.Append("insert into story(")
strSql.Append("title,contents,add_time)")
strSql.Append(" values (")
strSql.Append("@title,@contents,@add_time)")
strSql.Append("select @@IDENTITY")
SqlParameter[] parameters = {
new SqlParameter("@title", SqlDbType.NVarChar,550),
new SqlParameter("@contents", SqlDbType.NVarChar),
new SqlParameter("@add_time", SqlDbType.DateTime)}
parameters[0].Value = model.title
parameters[1].Value = model.contents
parameters[2].Value = model.add_time
object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters)
if (obj == null)
{
return 0
}
else
{
return Convert.ToInt32(obj)
}
}
/// <summary>
/// 更新一条数据
/// </summary>
public bool Update(BW.Model.story model)
{
StringBuilder strSql=new StringBuilder()
strSql.Append("update story set ")
strSql.Append("title=@title,")
strSql.Append("contents=@contents,")
strSql.Append("add_time=@add_time")
strSql.Append(" where id=@id")
SqlParameter[] parameters = {
new SqlParameter("@title", SqlDbType.NVarChar,550),
new SqlParameter("@contents", SqlDbType.NVarChar),
new SqlParameter("@add_time", SqlDbType.DateTime),
new SqlParameter("@id", SqlDbType.Int,4)}
parameters[0].Value = model.title
parameters[1].Value = model.contents
parameters[2].Value = model.add_time
parameters[3].Value = model.id
int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters)
if (rows >0)
{
return true
}
else
{
return false
}
}
/// <summary>
/// 删除一条数据
/// </summary>
public bool Delete(int id)
{
StringBuilder strSql=new StringBuilder()
strSql.Append("delete from story ")
strSql.Append(" where id=@id")
SqlParameter[] parameters = {
new SqlParameter("@id", SqlDbType.Int,4)
}
parameters[0].Value = id
int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters)
if (rows >0)
{
return true
}
else
{
return false
}
}
/// <summary>
/// 批量删除数据
/// </summary>
public bool DeleteList(string idlist )
{
StringBuilder strSql=new StringBuilder()
strSql.Append("delete from story ")
strSql.Append(" where id in ("+idlist + ") ")
int rows=DbHelperSQL.ExecuteSql(strSql.ToString())
if (rows >0)
{
return true
}
else
{
return false
}
}
/// <summary>
/// 得到一个对象实体
/// </summary>
public BW.Model.story GetModel(int id)
{
StringBuilder strSql=new StringBuilder()
strSql.Append("select top 1 id,title,contents,add_time from story ")
strSql.Append(" where id=@id")
SqlParameter[] parameters = {
new SqlParameter("@id", SqlDbType.Int,4)
}
parameters[0].Value = id
BW.Model.story model=new BW.Model.story()
DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters)
if(ds.Tables[0].Rows.Count>0)
{
if(ds.Tables[0].Rows[0]["id"]!=null &&ds.Tables[0].Rows[0]["id"].ToString()!="")
{
model.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString())
}
if(ds.Tables[0].Rows[0]["title"]!=null &&ds.Tables[0].Rows[0]["title"].ToString()!="")
{
model.title=ds.Tables[0].Rows[0]["title"].ToString()
}
if(ds.Tables[0].Rows[0]["contents"]!=null &&ds.Tables[0].Rows[0]["contents"].ToString()!="")
{
model.contents=ds.Tables[0].Rows[0]["contents"].ToString()
}
if(ds.Tables[0].Rows[0]["add_time"]!=null &&ds.Tables[0].Rows[0]["add_time"].ToString()!="")
{
model.add_time=DateTime.Parse(ds.Tables[0].Rows[0]["add_time"].ToString())
}
return model
}
else
{
return null
}
}
/// <summary>
/// 获得数据列表
/// </summary>
public DataSet GetList(string strWhere)
{
StringBuilder strSql=new StringBuilder()
strSql.Append("select id,title,contents,add_time ")
strSql.Append(" FROM story ")
if(strWhere.Trim()!="")
{
strSql.Append(" where "+strWhere)
}
return DbHelperSQL.Query(strSql.ToString())
}
/// <summary>
/// 获得前几行数据
/// </summary>
public DataSet GetList(int Top,string strWhere,string filedOrder)
{
StringBuilder strSql=new StringBuilder()
strSql.Append("select ")
if(Top>0)
{
strSql.Append(" top "+Top.ToString())
}
strSql.Append(" id,title,contents,add_time ")
strSql.Append(" FROM story ")
if(strWhere.Trim()!="")
{
strSql.Append(" where "+strWhere)
}
strSql.Append(" order by " + filedOrder)
return DbHelperSQL.Query(strSql.ToString())
}
/// <summary>
/// 获取记录总数
/// </summary>
public int GetRecordCount(string strWhere)
{
StringBuilder strSql=new StringBuilder()
strSql.Append("select count(1) FROM story ")
if(strWhere.Trim()!="")
{
strSql.Append(" where "+strWhere)
}
object obj = DbHelperSQL.GetSingle(strSql.ToString())
if (obj == null)
{
return 0
}
else
{
return Convert.ToInt32(obj)
}
}
/// <summary>
/// 分页获取数据列表
/// </summary>
public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
{
StringBuilder strSql=new StringBuilder()
strSql.Append("SELECT * FROM ( ")
strSql.Append(" SELECT ROW_NUMBER() OVER (")
if (!string.IsNullOrEmpty(orderby.Trim()))
{
strSql.Append("order by T." + orderby )
}
else
{
strSql.Append("order by T.id desc")
}
strSql.Append(")AS Row, T.* from story T ")
if (!string.IsNullOrEmpty(strWhere.Trim()))
{
strSql.Append(" WHERE " + strWhere)
}
strSql.Append(" ) TT")
strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex)
return DbHelperSQL.Query(strSql.ToString())
}
/*
/// <summary>
/// 分页获取数据列表
/// </summary>
public DataSet GetList(int PageSize,int PageIndex,string strWhere)
{
SqlParameter[] parameters = {
new SqlParameter("@tblName", SqlDbType.VarChar, 255),
new SqlParameter("@fldName", SqlDbType.VarChar, 255),
new SqlParameter("@PageSize", SqlDbType.Int),
new SqlParameter("@PageIndex", SqlDbType.Int),
new SqlParameter("@IsReCount", SqlDbType.Bit),
new SqlParameter("@OrderType", SqlDbType.Bit),
new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
}
parameters[0].Value = "story"
parameters[1].Value = "id"
parameters[2].Value = PageSize
parameters[3].Value = PageIndex
parameters[4].Value = 0
parameters[5].Value = 0
parameters[6].Value = strWhere
return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds")
}*/
#endregion Method
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)