1·绑定数据源来进行连接
创新互联公司是创新、创意、研发型一体的综合型网站建设公司,自成立以来公司不断探索创新,始终坚持为客户提供满意周到的服务,在本地打下了良好的口碑,在过去的十载时间我们累计服务了上千家以及全国政企客户,如格栅板等企业单位,完善的项目管理流程,严格把控项目进度与质量监控加上过硬的技术实力获得客户的一致称扬。
2.用代码连接
先到数据库建立一个数据库和相应的表
连接数据库的代码:
Dim str As String = "Data Source=服务器名;Initial Catalog=数据库名;Persist Security Info=True;User ID=;Password="
dim conn As SqlClient.SqlConnection
try
conn = New SqlClient.SqlConnection
conn.ConnectionString = str
conn.Open()
Return True
Catch ex As Exception
MsgBox(ex.ToString)
Return False
End Try
登录代码:Dim str As String = "Data Source=服务器名;Initial Catalog=数据库名;Persist Security Info=True;User ID=;Password="
dim conn As SqlClient.SqlConnection
conn = New SqlClient.SqlConnection
conn.ConnectionString = str
conn.Open()
sqlstr = "Select * From Amd Where AmdName='" TextBox1.Text "' And AmdPwd = '" TextBox2.Text "'"
Dim sqlcmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(sqlstr, conn)
Dim dr As SqlClient.SqlDataReader
dr = sqlcmd.ExecuteReader
If dr.Read = True Then '判断一条记录为真
kf.Show() '显示下个窗体
Me.Hide() ’隐藏当前窗体
Else
MessageBox.Show("输入信息有误!", "提示")
TextBox1.Text = ""
TextBox2.Text = ""
End If
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;
//数据库路径
lilDBPath.Text = HttpContext.Current.Request.PhysicalApplicationPath + "App_Data\\" + ConfigurationSettings.AppSettings["DBFile"];
//新建一个 FileInfo 对象,并获得数据库文件的大小,然后转换单位为KB
FileInfo myFileInfo = new FileInfo(lilDBPath.Text);
lilDBSize.Text = Convert.ToString(myFileInfo.Length/1024) + " KB";
//如果两个参数都不为空,则继续执行
if (Request.QueryString["cmd"] != null Request.QueryString["source"] != null)
{
//备份数据库原文件名
string sourceFileName = Request.QueryString["source"];
//如果 cmd 参数为 DelFile
if (Request.QueryString["cmd"].Equals("DelFile"))
{
//删除备份数据库文件
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'删除备份数据库成功!/spanimg src='images/aR.gif' style='margin-bottom:-6px;'//div";
}
//如果 cmd 参数为 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'还原备份数据库成功!/spanimg src='images/aR.gif' style='margin-bottom:-6px;'//div";
}
}
}
}
protected void lnkbtnCompactDB_Click(object sender, EventArgs e)
{
//压缩修复数据库
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'压缩修复数据库成功!/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";
//将数据库文件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'备份数据库成功!/spanimg src='images/aR.gif' style='margin-bottom:-6px;'//div";
}
/// summary
/// 生成备份文件列表
/// /summary
/// returns文件列表,文件详细信息及操作选项的HTML代码/returns
public void ListBackupFiles()
{
//如果目录不存在则创建次目录
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='删除备份文件'还原数据库/a | " + f.Length/1024 + " KB | " + f.CreationTime + "br /");
}
lilBackupFileList.Text = sb.ToString();
}
}
把下面这句换成你的数据库地址:
//数据库路径
// lilDBPath.Text = HttpContext.Current.Request.PhysicalApplicationPath + "App_Data\\" + ConfigurationSettings.AppSettings["DBFile"];
花了二十分钟给你写了代码,已测试。建议学习并使用System.Drawing绘制。
主要是掌握Graphics.FillRectangle和DrawString的使用。
Imports System.Drawing
Public Class 进度条UI
Public 上面笔刷 As SolidBrush = New SolidBrush(Color.FromArgb(192, 175, 238, 238))
Public 下面笔刷 As SolidBrush = New SolidBrush(Color.FromArgb(192, 30, 144, 255))
Public 文字笔 As SolidBrush = New SolidBrush(Color.FromArgb(255, 255, 255, 255))
Public 字体 As Font = New Font("微软雅黑", 14.0)
Public 文字格式 As StringFormat = New StringFormat() With
{.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center}
''' summary
''' 绘制指定进度的图像。
''' 当进度变化时调用一次本方法,建议将创建的Graphics对象保存到变量而不要重复创建。。
''' /summary
''' param name="控件"绘制到此控件的工作区/param
''' param name="g"绘制到控件的Graphics对象,例如 Button1.CreateGraphics()/param
''' param name="进度"进度百分比实数,57% = 0.57/param
Public Sub 绘制(ByRef 控件 As Control, ByRef g As Graphics, ByVal 进度 As Double)
Dim 矩形 = 控件.ClientRectangle '获取控件的工作区矩形
Dim 下面高度 = CInt(矩形.Height * 进度) '获取下面颜色块的高度
Dim 中间位置 = 矩形.Top + 矩形.Height - 下面高度 '获取中间分界线的Y坐标
Dim 上矩形 = New Rectangle(矩形.X, 矩形.Y, 矩形.Width, 矩形.Height - 下面高度)
Dim 下矩形 = New Rectangle(矩形.X, 中间位置, 矩形.Width, 下面高度)
g.FillRectangle(上面笔刷, 上矩形)
g.FillRectangle(下面笔刷, 下矩形)
'绘制文字
Dim 文字 As String = String.Format("{0:0.00}%", 进度 * 100)
g.DrawString(文字, 字体, 文字笔, 矩形, 文字格式)
End Sub
End Class
下面是Form1窗体的代码:添加一个Button1和Timer1控件,将Button1尺寸拖大点
Public Class Form1
Public g As Graphics
Public 进度条UI As New 进度条UI
Public 进度 As Double
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
g = Button1.CreateGraphics()
Timer1.Enabled = Not Timer1.Enabled
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
进度 += 0.01
进度条UI.绘制(Button1, g, 进度)
End Sub
End Class
Visual Studio 2013
Microsoft Visual Studio(简称VS)是美国微软公司的开发工具包系列产品。VS是一个基本完整的开发工具集,它包括了整个软件生命周期中所需要的大部分工具,如UML工具、代码管控工具、集成开发环境(IDE)等等。所写的目标代码适用于微软支持的所有平台,包括Microsoft Windows、Windows Mobile、Windows CE、.NET Framework、.NET Compact Framework和Microsoft Silverlight 及Windows Phone。 Visual Studio是目前最流行的Windows平台应用程序的集成开发环境。最新版本为 Visual Studio 2013 版本,基于.NET Framework 4.5.1 。
如果楼主熟悉VB6,可以直接在项目中添加ADODB的Com引用,这样你就可以像VB6那样操作数据库了!
另外
.NET
Framework中连接数据库要用到ADO.NET。如果要操作Access数据库,要用到System.Data.OleDb命名空间下的许多类。
比如按楼主所说,“我想在textbox1中显示表一中【一些数据】字段下的第一个内容”:
'首先导入命名空间
Imports
System.Data
Imports
System.Data.OleDb
'然后在某一个事件处理程序中写:
Dim
conn
As
New
OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=数据库.accdb;Jet
OLEDB:Database
Password=MyDbPassword")
Dim
command
As
New
OleDbCommand("Select
*
From
数据表",
conn)
conn.Open()
'打开数据库连接
Dim
reader
As
OleDbDataReader
=
command.ExecuteReader()
'执行SQL语句,返回OleDbDataReader
对象
Do
While
reader.Read()
'读取一条数据
textbox1.Text
+=
reader("一些数据")
VbCrLf
Loop
reader.Close()
'关闭OleDbDataReader
conn.Close()
'关闭连接