vb.net代码库 vb·net
vb.net 连接SQL数据库登录代码
问题应该出在你的查询语句的结果没有值,你用你的查询语句到SQL中是否有返回值?记得加上你的条件,另外,你语句里这一句是多余的.Dim s As Object = sComm.ExecuteScalar 以下语句测试正常 Dim myCn As New SqlClient.SqlConnection Try myCn.ConnectionString = sConnString myCn.Open() Dim mySQL As String = "SELECT * FROM ygzl" Dim sComm As New SqlClient.SqlCommand(mySQL, myCn) If sComm.ExecuteScalar Then Dim User_MainForm As New Form1 User_MainForm.Show() Else MsgBox("用户名或密码不正确,请重试!") End If Catch ex As Exception MsgBox(ex.Message) Finally myCn.Close() '关闭连接 End Try
在甘泉等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站建设、网站设计 网站设计制作定制网站建设,公司网站建设,企业网站建设,成都品牌网站建设,成都全网营销推广,外贸营销网站建设,甘泉网站建设费用合理。
求VB.NET2010操作ACCESS数据库的完整代码
Imports System.Data.OleDb
Public Class Parking
Private Sub Parking_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Now_Timer.Enabled = True
End Sub
Private Sub Now_Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Now_Timer.Tick
Now_Time_Label.Text = "当前时间:" Date.Now
End Sub
Private Sub Enter_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Enter_Button.Click
'定义一个OLEDB连接字符串
Dim conStr As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=F:\test\test.mdb"
'实例化OLEDB连接
Dim con As OleDbConnection = New OleDbConnection(conStr)
Dim sql As New System.Text.StringBuilder
'定义数据库插入语句
sql.Append("insert into Time_billing([Car_Num],[Enter_Time])")
sql.Append("values('" Trim(Car_Num_Text.Text) "','" Date.Now "')")
'打开数据库链接
con.Open()
'定义执行命令
Dim cmd As New System.Data.OleDb.OleDbCommand(sql.ToString, con)
'执行命令
cmd.ExecuteNonQuery()
'关闭数据库链接
con.Close()
MsgBox("提交成功!")
End Sub
Private Sub Leave_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Leave_Button.Click
Dim Time_Length As Double
Dim Pack_Fee As Double
Dim Enter_time As Date
Dim Leave_time As Date
Dim conStr As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=F:\test\test.mdb"
Dim con As OleDbConnection = New OleDbConnection(conStr)
Dim selSql As New System.Text.StringBuilder
Dim inSql As New System.Text.StringBuilder
Dim upSql As New System.Text.StringBuilder
Dim delSql As New System.Text.StringBuilder
Dim dr As OleDbDataReader
con.Open()
'SQL拼接过程中最后不需要有分号
'在赋值时,读取字段值时必须使用在数据库客户端查询时显示的字段名
selSql.Append("select")
selSql.Append(" Enter_Time")
selSql.Append(" from [Time_billing]")
selSql.Append("where Car_Num = '").Append(Trim(Car_Num_Text.Text)).Append("'")
Dim selcmd As New OleDb.OleDbCommand(selSql.ToString, con)
dr = selcmd.ExecuteReader()
If dr.Read() Then
Enter_time = dr("Enter_Time")
Leave_time = Date.Now
Else
MsgBox("木有数据!")
End If
Enter_Time_Text.Text = Enter_time
Leave_Time_Text.Text = Leave_time
'求时间差
Time_Length = Math.Round(DateDiff(DateInterval.Minute, Enter_time, Leave_time) / 60, 2)
Pack_Fee = Time_Length * 5
Pack_Fee_Text.Text = Pack_Fee
inSql.Append("update [Time_billing]")
inSql.Append(" set [Leave_Time] = '").Append(Trim(Leave_Time_Text.Text)).Append("'")
inSql.Append(" ,[Packing_Fee] = '").Append(Pack_Fee).Append("'")
inSql.Append("where Car_Num = '").Append(Trim(Car_Num_Text.Text)).Append("'")
Dim incom As New OleDb.OleDbCommand(inSql.ToString, con)
incom.ExecuteNonQuery()
'con.Close()
MsgBox("结算完成!")
'con.Open()
upSql.Append("insert into Time_billing_History([Car_Num],[Enter_Time],[Leave_Time],[Packing_Fee])")
upSql.Append(" select")
upSql.Append(" [Car_Num]")
upSql.Append(",[Enter_Time]")
upSql.Append(",[Leave_Time]")
upSql.Append(",[Packing_Fee]")
upSql.Append(" from [Time_billing]")
upSql.Append("where [Car_Num] = '").Append(Trim(Car_Num_Text.Text)).Append("'")
Dim upcom As New OleDb.OleDbCommand(upSql.ToString, con)
upcom.ExecuteNonQuery()
delSql.Append("delete")
delSql.Append(" from")
delSql.Append(" [Time_billing]")
delSql.Append(" where [Car_Num] = '").Append(Trim(Car_Num_Text.Text)).Append("'")
Dim delcom As New OleDb.OleDbCommand(delSql.ToString, con)
delcom.ExecuteNonQuery()
con.Close()
End Sub
Private Sub Clear_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clear_Button.Click
Car_Num_Text.Clear()
Enter_Time_Text.Clear()
Leave_Time_Text.Clear()
Pack_Fee_Text.Clear()
End Sub
End Class
vb.net2015连接数据库SQL2012代码
1、 用The SQL Server .NET Data Provider连接数据库
The SQL Server .NET Data Provider是利用SqlConnection类来连接SQL Server7.0或更高版本的数据库,
SqlConnection类位于名称空间System.Data.SqlClient下。
连接代码:
Dim sqlConnection1 As SqlClient.SqlConnection
Dim strConnect As String=”data source=服务器名;initial catalog=数据库名;user id=sa;password=;”
sqlConnection1=New System.Data.SqlClient.SqlConnection(strConnect)
sqlConnection1.open ‘打开数据库
sqlConnection1.close ‘关闭连接,释放资源
2、 用The OLE DB .NET Data Provider连接数据库
上面已经说过,利用The OLE DB .NET Data Provider可以访问Access、Oracle和SQL Server等种数据
库,那么,它是怎样访问这些数据库的呢?The OLE DB .NET Data Provider是通过位于名称空间Sy
stem.Data.OleDb类库下的OleDbConnection类来连接这三种不同类型的数据库的。下面举例说明:
1)连接SQL Server数据库
Dim oleDbConnection1 As OleDb.OleDbConnection
Dim strConnect As Sting=”Provider=SQLOLEDB;Persist Security Info=False;Data Source=服务器名;Initial Catalog=数据库名;User ID=sa;Password=;”
oleDbConnection1=New System.Data.OleDb.OleDbConnection(strConnect)
2)连接Access数据库
假设要连接的Access数据库名为“Example.mdb”,存放在d:\Data\目录下。
Dim oleDbConnection1 As OleDb.OleDbConnection
Dim strConnect As Sting=”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\Data\ Example.mdb”
oleDbConnection1= New System.Data.OleDb.OleDbConnection(strConnect)
3)连接Oracle数据库
Dim oleDbConnection1 As OleDb.OleDbConnection
Dim strConnect As Sting=”Provider=MSDAORA;Data Source=服务器名;User ID=用户ID;Password=密码;”
oleDbConnection1= New System.Data.OleDb.OleDbConnection(strConnect)
3、 用The ODBC .NET Data Provider连接数据库
The ODBC .NET Data Provider连接数据库是通过OdbcConnection类来实现的,这个类位于名称空间
Microsoft.Data.Odbc下,而名称空间Microsoft.Data.Odbc是封装在Microsoft.Data.Odbc.dll文件下的。
由于篇幅有限,这里就只介绍连接Sql Server和Oracle数据库的方法,其他数据库的连接方法基本类
似,我就不再多讲了。
1)连接Sql Server数据库
Dim odbcDbConnetion1 As Microsoft.Data.OdbcConnection
Dim strConnect As Sting=”Driver={SQL Server};Server=服务器名;Uid=sa;pwd=;Database= 数据库名;”
odbcDbConnetion1=New Microsoft.Data.OdbcConnection(strConnect)
2)连接Oracle数据库
Dim odbcDbConnetion1 As Microsoft.Data.OdbcConnection
Dim strConnect As Sting=”Driver={Microsoft ODBC for Oracle};Server=服务器名;Uid=sa;pwd=;”
odbcDbConnetion1=New Microsoft.Data.OdbcConnection(strConnect)
四、总结
通过本文的介绍,读者基本掌握了在Visual Basic.NET中用ADO.NET和ODBC.NET连接各种数据库的方法
。以上三种驱动针对不同的数据库,它们的性能方面也有很大的不同:The SQL Server .NET Data Provider
的效率最高;The OLE DB .NET Data Provider的效率比较底;The ODBC .NET Data Provider的效率最慢。
使用VB.net实现sql数据库的备份与恢复代码
Private Sub Button _Click(ByVal sender As System Object ByVal e As System EventArgs) Handles Button Click 备份数据库 BACKUP DATABASE Test TO DISK= C:\Backup WITH Format Dim cn As SqlConnection Dim cm As SqlCommand cn = getDB() cm = New SqlCommand( BACKUP DATABASE JXbase TO DISK= C:\Backup WITH Format cn) cn Open() cm ExecuteNonQuery() MsgBox( 备份成功 ) cn Close()
End Sub
恢复 Private Sub Button _Click(ByVal sender As System Object ByVal e As System EventArgs) Handles Button Click Dim cn As New SqlConnection Dim cn As New SqlConnection Dim mydr As SqlDataReader Dim str As String
Dim tmpConnectionString As String = Data Source=JJK;Initial Catalog=master;Integrated Security=True cn ConnectionString = tmpConnectionString cn ConnectionString = tmpConnectionString cn Open() cn Open() Dim cm As SqlCommand = New SqlCommand( select spid from master sysprocesses where dbid=db_id( JXbase ) cn) mydr = cm ExecuteReader()
Dim cm As SqlCommand = New SqlCommand() cm Connection = cn
While (mydr Read()) 杀死进程
str = kill mydr( spid ) ToString() cm CommandText = str cm CommandType = CommandType Text Application DoEvents() cm ExecuteNonQuery() 杀进程 End While mydr Close() cm = New SqlCommand( RESTORE DATABASE JXbase FROM DISK= C:\Backup cn) 恢复备份 cm ExecuteNonQuery() MsgBox( 恢复成功成功 ) cn Close() cn Close()
lishixinzhi/Article/program/net/201311/12901
求vb.net的源代码,最好说明其解决问题,越多越好,满意加50分。
下面这段代码,是我用来计算每个月存500元进银行,连续30年,最后连本带利能有多少钱。这里面涉及复利计算。界面中右边的文本框用来输出每一次计算的结果。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim nianxian As Integer '年限变量
Dim dingcun As Integer '定存变量
Dim fuli_big As Long '大复利
Dim fuli_small As Long '小复利
Dim i As Integer '循环变量
Dim DATAstring As String '数据字符串
nianxian = Val(年限_TextBox.Text)
dingcun = Val(定存_TextBox.Text)
DATAstring = ""
For i = 1 To nianxian
fuli_small = dingcun * (1 + 0.1875)
dingcun = fuli_small
fuli_big = fuli_big + fuli_small
DATAstring = DATAstring + "[" + Trim(Str(i)) + "]" + Str(fuli_big) + Chr(13) + Chr(10)
'DATAstring = DATAstring + "[" + Trim(Str(i)) + "]" + Str(fuli_small) + Chr(13) + Chr(10)
Next
'fuli_big = fuli_small
TextBox1.Text = DATAstring
结果_TextBox.Text = Str(fuli_big) + "元"
End Sub
VB.NET中的代码
大概的过程:
Imports System.Data
Imports System.Data.SqlClient
--------------
Dim a As New SqlDataAdapter
Dim con As SqlConnection
con = New SqlConnection( "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=.;Connect Timeout=30")
con.Open()
a.SelectCommand = New SqlCommand("select * from table where c=1", con)
a.UpdateCommand = New SqlCommand("update table set b=2 where c=1", con)
a.DeleteCommand = New SqlCommand("delete from table where c=1", con)
a.InsertCommand = New SqlCommand("insert into table values (1,2,3)", con)
a.SelectCommand.ExecuteNonQuery()
a.UpdateCommand.ExecuteNonQuery()
a.DeleteCommand.ExecuteNonQuery()
a.InsertCommand.ExecuteNonQuery()
con.Close()
a.Dispose()
分享名称:vb.net代码库 vb·net
文章出自:http://azwzsj.com/article/hhpiso.html