vb.net的数据库连接
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vb.net的数据库连接相关的知识,希望对你有一定的参考价值。
vb.net怎么和数据库连接,vb.net用的是设计,数据库用server的。连接数据库是必须用编程代码实现?
比如说:Vb.net的登陆怎么用连数据库,实现。。。
有谁会请告诉我,谢谢阿~(我目前全部的分都给了)
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本回答被提问者采纳 参考技术B 给你一个函数 直接调用就行了,
Public Conn As String = "server=服务器;database=数据库名;uid=账号;pwd=密码"
Public connection As SqlClient.SqlConnection
Public Function DBopen() As Boolean '打开数据库
Try
connection = New SqlClient.SqlConnection
connection.ConnectionString = Conn
connection.Open()
Return True
Catch ex As Exception
MsgBox(ex.ToString)
Return False
End Try
End Function
调用 Call DBopen()就可以啦
登录代码 吧加码加入到button 事件中。自己分析代码建立表字段
Dim sqlstr As String
If TextBox1.Text = "" And TextBox2.Text <> "" Then
MsgBox("您还没有输入账号哦!", , "提示!")
ElseIf TextBox1.Text <> "" And TextBox2.Text = "" Then
MsgBox("您还没有输入密码!", , "提示!")
ElseIf TextBox1.Text = "" And TextBox2.Text = "" Then
MsgBox("请输入账号密码!", , "提示!")
Else
Call DBopen() 'sqlstr = "select * from admin where username=@name and password=@pwd"
sqlstr = "select * from Waa_YuanGong where Name='" & Trim(TextBox1.Text) & "'and pass='" & Trim(TextBox2.Text) & "'" '我创建一个SQLDataAdapter对象
Dim da As New SqlDataAdapter(sqlstr, connection) '创建一个DataSet才对象
Dim ds As New DataSet() 'connection.Open()
da.Fill(ds) ' connection.Close()
t = ds.Tables(0)
If ds.Tables(0).Rows.Count = 0 Then 'DataSet1已填充,判断DataSet1数据集中是否有记录,如果没有报错。否则,进入 下一个界面
MsgBox("你输入的帐号或密码有误!", , "提示")
Else
userid = t.Rows(0).Item(1).ToString
user_Pwd = t.Rows(0).Item(2).ToString
Form2.Show()
Me.Hide()
End If
End If
参考资料:http://www.waa.cn
参考技术C Dim Connectstringftp As StringConnectstringftp = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath() & "\db1.mdb"
Dim sqlstr As String
objSqlConnection = Nothing
objSqlConnection = New OleDbConnection(Connectstringftp)
sqlstr = "select * from CityName"
Dim myDS1 As New DataSet
myDS1.Clear()
myDS1 = GetDataFromDB(sqlstr)
YHM1 = Convert.ToString(myDS1.Tables(0).Rows(0)("CwServerName"))
MM1 = Convert.ToString(myDS1.Tables(0).Rows(0)("CwUserName"))
If Convert.ToString(myDS1.Tables(0).Rows(0)("CwUserPassword")) <> "" Then
JL1 = Convert.ToString(myDS1.Tables(0).Rows(0)("CwUserPassword"))
End If
If Convert.ToString(myDS1.Tables(0).Rows(0)("CwUserPassword2")) <> "" Then
JL2 = Convert.ToString(myDS1.Tables(0).Rows(0)("CwUserPassword2"))
End If
CheckBox1.Checked = JL1
CheckBox2.Checked = JL2
If CheckBox1.Checked = True Then
TextBox1.Text = YHM1
End If
If CheckBox2.Checked = True Then
TextBox2.Text = MM1
End If
If Trim(Me.TextBox1.Text) = "" Then
MessageBox.Show("用户名不能为空!", "系统信息", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
ElseIf Trim(Me.TextBox2.Text) = "" Then
MessageBox.Show("密码不能为空!", "系统信息", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
Dim sqlstr As String
objSqlConnection = Nothing
objSqlConnection = New OleDbConnection(ConnectString)
sqlstr = "select * from USERID where YHM='" & Trim(TextBox1.Text) & "'AND MM='" & Trim(TextBox2.Text) & "'AND BM='" & CityName & "'"
Dim myDS1 As New DataSet
myDS1.Clear()
myDS1 = GetDataFromDB(sqlstr)
If myDS1.Tables(0).DefaultView.Count - 1 >= 0 Then
'MsgBox(myDS1.Tables(0).DefaultView.Count)
YHM = Convert.ToString(myDS1.Tables(0).Rows(0)("YHM"))
MM = Convert.ToString(myDS1.Tables(0).Rows(0)("MM"))
BM = Convert.ToString(myDS1.Tables(0).Rows(0)("BM"))
End If
If Trim(TextBox1.Text) <> YHM Then
MsgBox("用户名或密码错误!")
Exit Sub
ElseIf Trim(TextBox2.Text) <> MM Then
MsgBox("用户名或密码错误!")
Exit Sub
End If 参考技术D using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Drawing;
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.Data.SqlClient;
using System.IO;
namespace UserData
public class Userdo
string strConn = "uid=sa;pwd=;database=tu;server=";
string errstr = null;
protected static void Main()
public string adduser(string username, string nickname, string password, string email, string major)
string query = "insert into userinfo(username,nickname,password,email,major) values ( @username,@nickname,@password,@email,@major)";
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = query;
cmd.Connection = conn;
SqlParameter sp1 = new SqlParameter("@username", SqlDbType.Char, 255);
sp1.Direction = ParameterDirection.Input;
cmd.Parameters.Add(sp1);
SqlParameter sp2 = new SqlParameter("@nickname", SqlDbType.Char, 255);
sp2.Direction = ParameterDirection.Input;
cmd.Parameters.Add(sp2);
SqlParameter sp3 = new SqlParameter("@password", SqlDbType.Char, 255);
sp3.Direction = ParameterDirection.Input;
cmd.Parameters.Add(sp3);
SqlParameter sp4 = new SqlParameter("@email", SqlDbType.Char, 255);
sp4.Direction = ParameterDirection.Input;
cmd.Parameters.Add(sp4);
SqlParameter sp5 = new SqlParameter("@major", SqlDbType.Char, 255);
sp5.Direction = ParameterDirection.Input;
cmd.Parameters.Add(sp5);
sp1.Value = username;
sp2.Value = nickname;
sp3.Value = password;
sp4.Value = email;
sp5.Value = major;
try
cmd.ExecuteNonQuery();
catch (Exception ee)
if (ee != null)
errstr = ee.Message;
cmd.Dispose();
conn.Close();
return errstr;
连接数据库
新建一个ASP.NET项目。将服务器资源管理器中的数据连接拖至新建的Web Form上即可,这时自动生成了一个连接字符串如下:
this.sqlConnection1.ConnectionString = "data source=jeff;initial catalog=tyjdb;persist security info=False;user id=sa;workstation id=JEFF;packet size=4096";
其中jeff为笔者的服务器名,sa为服务器登录名。
选取工具箱中的SqlDataAdapter并拖至该Web Form,提示时选取tyjdb的数据连接,选择使用SQL语句访问数据库,生成SQL语句时只填入SELECT * FROM ADDRESS,确认完成即可。程序生成代码如下:
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
//存取数据库的主要类
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
//SQL语句处理的类
protected System.Data.SqlClient.SqlConnection sqlConnection1;
//连接数据库的类
在InitializeComponent()中有如下声明:
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlSelectCommand1.CommandText = "SELECT name, email, age, address FROM address";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
为使Table中数据能在Web Form中显示,加入一个DataGrid控件至Web Form上,并在Page_Init中加入如下语句:
sqlConnection1.Open();
//打开数据库连接
DataSet objDataset;
//新建一个放数据的DataSet
objDataset=new DataSet();
sqlDataAdapter1.Fill(objDataset, "address");
//将数据填入DataSet
DataGrid1.DataSource=objDataset.Tables["address"].DefaultView;
//关联DataSet和DataGrid
DataGrid1.DataBind();
//绑定数据
sqlConnection1.Close();
//关闭数据库连接
编译执行后Web Form已可将数据库中数据显示在DataGrid中了。
以上是关于vb.net的数据库连接的主要内容,如果未能解决你的问题,请参考以下文章