用ADO接口就可以,TCP/IP協(xié)議,設(shè)定數(shù)據(jù)庫服務(wù)器的IP地址,數(shù)據(jù)庫名,用戶名,密碼就可以連接。
成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比開化網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式開化網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋開化地區(qū)。費(fèi)用合理售后完善,十多年實(shí)體公司更值得信賴。
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using _5dRss.Const;
using _5dRss.lib.Data.Tool;
public partial class admin_admin_dbmanage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//生成備份文件列表
ListBackupFiles();
if (!Page.IsPostBack)
{
Showmsg.Visible = false;
//數(shù)據(jù)庫路徑
lilDBPath.Text = HttpContext.Current.Request.PhysicalApplicationPath + "App_Data\\" + ConfigurationSettings.AppSettings["DBFile"];
//新建一個(gè) FileInfo 對(duì)象,并獲得數(shù)據(jù)庫文件的大小,然后轉(zhuǎn)換單位為KB
FileInfo myFileInfo = new FileInfo(lilDBPath.Text);
lilDBSize.Text = Convert.ToString(myFileInfo.Length/1024) + " KB";
//如果兩個(gè)參數(shù)都不為空,則繼續(xù)執(zhí)行
if (Request.QueryString["cmd"] != null Request.QueryString["source"] != null)
{
//備份數(shù)據(jù)庫原文件名
string sourceFileName = Request.QueryString["source"];
//如果 cmd 參數(shù)為 DelFile
if (Request.QueryString["cmd"].Equals("DelFile"))
{
//刪除備份數(shù)據(jù)庫文件
File.Delete(HttpContext.Current.Request.PhysicalApplicationPath + "Backup\\" + sourceFileName);
//刷新備份文件列表
ListBackupFiles();
Showmsg.Visible = true;
Showmsg.Text = "div align='center' style='margin-bottom:8px;'img src='images/aL.gif' style='margin-bottom:-6px;'/span class='alertTxt'刪除備份數(shù)據(jù)庫成功!/spanimg src='images/aR.gif' style='margin-bottom:-6px;'//div";
}
//如果 cmd 參數(shù)為 Restore
if (Request.QueryString["cmd"].Equals("Restore"))
{
//用備份文件覆蓋原文件
File.Copy(HttpContext.Current.Request.PhysicalApplicationPath + "Backup\\" + sourceFileName, HttpContext.Current.Request.PhysicalApplicationPath + "App_Data\\" + ConfigurationSettings.AppSettings["DBFile"], true);
//刷新備份文件列表
ListBackupFiles();
Showmsg.Visible = true;
Showmsg.Text = "div align='center' style='margin-bottom:8px;'img src='images/aL.gif' style='margin-bottom:-6px;'/span class='alertTxt'還原備份數(shù)據(jù)庫成功!/spanimg src='images/aR.gif' style='margin-bottom:-6px;'//div";
}
}
}
}
protected void lnkbtnCompactDB_Click(object sender, EventArgs e)
{
//壓縮修復(fù)數(shù)據(jù)庫
AccessDBtool.CompactAccessDB(SysConfig.ConnectionString, HttpContext.Current.Request.PhysicalApplicationPath + "App_Data\\" + ConfigurationSettings.AppSettings["DBFile"]);
Showmsg.Visible = true;
Showmsg.Text = "div align='center' style='margin-bottom:8px;'img src='images/aL.gif' style='margin-bottom:-6px;'/span class='alertTxt'壓縮修復(fù)數(shù)據(jù)庫成功!/spanimg src='images/aR.gif' style='margin-bottom:-6px;'//div";
}
protected void lnkbtnBackupDB_Click(object sender, EventArgs e)
{
string sourceFileName = HttpContext.Current.Request.PhysicalApplicationPath + "App_Data\\" + ConfigurationSettings.AppSettings["DBFile"];
string destFileName = HttpContext.Current.Request.PhysicalApplicationPath + "Backup\\" + "Backup_";
destFileName += DateTime.Now.ToString("yyyyMMddHHmmss");
destFileName += ".mbk";
//將數(shù)據(jù)庫文件Copy到Backup目錄,如果有重名文件就覆蓋原文件
File.Copy(sourceFileName, destFileName, true);
//生成備份文件列表
ListBackupFiles();
Showmsg.Visible = true;
Showmsg.Text = "div align='center' style='margin-bottom:8px;'img src='images/aL.gif' style='margin-bottom:-6px;'/span class='alertTxt'備份數(shù)據(jù)庫成功!/spanimg src='images/aR.gif' style='margin-bottom:-6px;'//div";
}
/// summary
/// 生成備份文件列表
/// /summary
/// returns文件列表,文件詳細(xì)信息及操作選項(xiàng)的HTML代碼/returns
public void ListBackupFiles()
{
//如果目錄不存在則創(chuàng)建次目錄
if (!Directory.Exists(HttpContext.Current.Request.PhysicalApplicationPath + "Backup\\"))
Directory.CreateDirectory(HttpContext.Current.Request.PhysicalApplicationPath + "Backup\\");
DirectoryInfo mydir = new DirectoryInfo(HttpContext.Current.Request.PhysicalApplicationPath + "Backup\\");
StringBuilder sb = new StringBuilder();
foreach (FileInfo f in mydir.GetFiles())
{
sb.Append("a href='backup/" + f.Name + "' target='_blank'img border='0' src='images/mdb.gif' style='margin:4px 3px -3px 0px'/" + f.Name + "/a a href='?cmd=DelFilesource=" + f.Name + "' title='刪除備份文件'刪除/a | a href='?cmd=Restoresource=" + f.Name + "' title='刪除備份文件'還原數(shù)據(jù)庫/a | " + f.Length/1024 + " KB | " + f.CreationTime + "br /");
}
lilBackupFileList.Text = sb.ToString();
}
}
把下面這句換成你的數(shù)據(jù)庫地址:
//數(shù)據(jù)庫路徑
// lilDBPath.Text = HttpContext.Current.Request.PhysicalApplicationPath + "App_Data\\" + ConfigurationSettings.AppSettings["DBFile"];
如果樓主熟悉VB6,可以直接在項(xiàng)目中添加ADODB的Com引用,這樣你就可以像VB6那樣操作數(shù)據(jù)庫了!
另外
.NET
Framework中連接數(shù)據(jù)庫要用到ADO.NET。如果要操作Access數(shù)據(jù)庫,要用到System.Data.OleDb命名空間下的許多類。
比如按樓主所說,“我想在textbox1中顯示表一中【一些數(shù)據(jù)】字段下的第一個(gè)內(nèi)容”:
'首先導(dǎo)入命名空間
Imports
System.Data
Imports
System.Data.OleDb
'然后在某一個(gè)事件處理程序中寫:
Dim
conn
As
New
OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=數(shù)據(jù)庫.accdb;Jet
OLEDB:Database
Password=MyDbPassword")
Dim
command
As
New
OleDbCommand("Select
*
From
數(shù)據(jù)表",
conn)
conn.Open()
'打開數(shù)據(jù)庫連接
Dim
reader
As
OleDbDataReader
=
command.ExecuteReader()
'執(zhí)行SQL語句,返回OleDbDataReader
對(duì)象
Do
While
reader.Read()
'讀取一條數(shù)據(jù)
textbox1.Text
+=
reader("一些數(shù)據(jù)")
VbCrLf
Loop
reader.Close()
'關(guān)閉OleDbDataReader
conn.Close()
'關(guān)閉連接
首先你要測(cè)試下你的程序能不能連接到數(shù)據(jù)庫;
若數(shù)據(jù)庫連接成功了,那么就是insert into 語句有問題;
在調(diào)試模式下,你插入的數(shù)據(jù)庫內(nèi)容通過查表是看不出來的;內(nèi)容沒有寫入到表中;
建立數(shù)據(jù)集,綁定到窗口。不會(huì)的話,把要用的表從數(shù)據(jù)集拖到窗口,再刪除就行了
提取方法
數(shù)據(jù)及名稱.表格名稱.rows(2).item(3).tostring 就是了