VB中怎样保存数据(保存在VB数据库里)?

2024-11-17 01:32:12
推荐回答(4个)
回答1:

1.使李亩用文件来记录,下次运行时读取 .2.使用数据库,可以考虑单机数据库或网络数据库 3.如果数据量小,使用注册表.
或者可以通过程序来保存:
set fs=createobject("scripting.filesystemobject")'创建文件操作对象
if fs.fileexists("C:\a.txt") then'如果C盘A.txt存在,那么读取其内容
set txt=fs.opentextfile("C:\a.txt",forreading,true)
set txtn=txt.readall
else'
set txtf=fs.createtextfile("c:\a.txt",true)'创建C盘下的a.txt
do'无限循环
wscript.sleep 1000'哪缓森延时一秒
txtf.write a.text'写入文本框中内容
loop'循哪粗环结束
end if'判断结束

回答2:

步骤
1、 先来看一下northwind数据库中employees表结构

2、 知道.net连接sql server的方法嘛,不知道的话,看看我写的“ado.net中的connection对象”吧。读懂下面这几句没问题吧:
Dim conn as new sqlconnection(“server=localhost;database=northwind;Integrated Security=true;”
Dim sqlcomm As New SqlCommand
sqlcomm.Connection = conn
sqlcomm.CommandText = "INSERT INTO employees (lastname,firstname,photo) VALUES (@lastname,@firstname,@photo)"
Dim prm1 As New SqlParameter("@lastname", txtLN.Text)
Dim prm2 As New SqlParameter("@firstname", txtFN.Text)
Dim prm3 As New SqlParameter("@photo", SqlDbType.VarBinary, Int(fs.Length), _ ParameterDirection.Input, False, 0, 0, "", DataRowVersion.Current, Data)
sqlcomm.Parameters.Add(prm1)
sqlcomm.Parameters.Add(prm2)
sqlcomm.Parameters.Add(prm3)
3 、新建一个windows应用程序窗体
4、 声明类级的变量。找到设计期产生的谨锋代码部分,把下面的语祥坦晌句加到变量声明后面。什么?不知道变量声明在哪儿?不会吧!
Private fs As FileStream
Private ds As DataSet
Private conn As New SqlConnection("server=localhost;database=northwind;integrated security=true;")
Private currentpos As Integer = 9
5、 开始写代码了。首先是信谈form_load
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
btnOpen.Enabled = True
btnSave.Enabled = True
btnBack.Enabled = False
btnForward.Enabled = False
End Sub
6、 open按钮的点击事件代码:
Private Sub openbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
Dim Opendlg As New OpenFileDialog
Opendlg.Title = "Select a picture file"
Opendlg.Filter = "(*.bmp)|*bmp|(*.jpg)|*.jpg"
Opendlg.ShowDialog()
lblPath.Text = Opendlg.FileName.ToString()
fs = New FileStream(Opendlg.FileName.ToString(), FileMode.Open, FileAccess.Read)
PictureBox1.Image = Image.FromFile(Opendlg.FileName.ToString())
End Sub
7、 save按钮的点击事件代码
Private Sub savebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim sqlcomm As New SqlCommand
sqlcomm.CommandText = "INSERT INTO employees (lastname,firstname,photo) VALUES (@lastname,@firstname,@photo)"
sqlcomm.Connection = conn
Dim Data(fs.Length) As Byte
fs.Read(Data, 0, Int(fs.Length))
Dim prm1 As New SqlParameter("@lastname", txtLN.Text)
Dim prm2 As New SqlParameter("@firstname", txtFN.Text)
Dim prm3 As New SqlParameter("@photo", SqlDbType.VarBinary, Int(fs.Length), ParameterDirection.Input, False, 0, 0, "", DataRowVersion.Current, Data)
sqlcomm.Parameters.Add(prm1)
sqlcomm.Parameters.Add(prm2)
sqlcomm.Parameters.Add(prm3)
Try
conn.Open()
sqlcomm.ExecuteNonQuery() '执行插入语句
conn.Close()
fs.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
8、 选择view状态的事件代码
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
btnOpen.Enabled = False
btnSave.Enabled = False
btnBack.Enabled = True
btnForward.Enabled = True
currentpos = 9
Dim sqlcomm As New SqlCommand
sqlcomm.CommandText = "SELECT employeeId, photo FROM employees ORDER BY employeeId"
sqlcomm.Connection = conn
Dim da As New SqlDataAdapter(sqlcomm)
Try
conn.Open()
ds = New DataSet
da.Fill(ds, "employees")
conn.Close()
Catch sqlEx As SqlException
MsgBox(sqlEx.Message)
End Try
Dim data() As Byte = ds.Tables("employees").Rows(9)("photo")
Dim stmphoto As New MemoryStream(data)
PictureBox1.Image = Image.FromStream(stmphoto)
Else
btnOpen.Enabled = True
btnSave.Enabled = True
btnBack.Enabled = False
btnForward.Enabled = False
End If
End Sub
9、 “>>”按钮点击事件代码
Private Sub forward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForward.Click
If currentpos = ds.Tables("employees").Rows.Count - 1 Then
Return
Else
currentpos += 1
Dim data() As Byte
data = ds.Tables("employees").Rows(currentpos)("photo")
Dim stmphoto As New MemoryStream(data)
PictureBox1.Image = Image.FromStream(stmphoto)
End If
End Sub
10、 “<<”按钮点击事件代码
Private Sub back_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
If currentpos = 9 Then
Return
Else
currentpos -= 1
End If
Dim Data() As Byte
Data = ds.Tables("employees").Rows(currentpos)("photo")
Dim stmPhoto As New MemoryStream(Data)
PictureBox1.Image = Image.FromStream(stmPhoto)
End Sub
来源于百度百科

回答3:

方法一:在数据库中建笑返一个表,把当前最后余茄的碰毁饥输入值保存在该表里,,每次执行程序都从数据库里读取,
方法二:在程序中建一个文本文件或xml文件,每次都写入文件中,下次从文件中读取
方法三:把输入值存入注册表的某个位置,下次从注册表里读取!

回答4:

set fs=createobject("scripting.filesystemobject")'创建文件操作对象
if fs.fileexists("C:\a.txt") then'如果C盘A.txt存在,那么读取其内容
set txt=fs.opentextfile("C:\a.txt",forreading,true)
set txtn=txt.readall
else'如果不是
set txtf=fs.createtextfile("c:\a.txt",true)'创建C盘下的a.txt
do'无限循环(不用担心他一直循环,关了就没了)
wscript.sleep 1000'延时一秒
txtf.write a.text'写入文本凯团族框中内容
loop'循环结束
end if'判断结束

注意啦!以上代码未盯弊经过或亮测试!只是一个思路。