問(wèn)題應(yīng)該出在你的查詢語(yǔ)句的結(jié)果沒(méi)有值,你用你的查詢語(yǔ)句到SQL中是否有返回值?記得加上你的條件,另外,你語(yǔ)句里這一句是多余的.Dim s As Object = sComm.ExecuteScalar 以下語(yǔ)句測(cè)試正常 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("用戶名或密碼不正確,請(qǐng)重試!") End If Catch ex As Exception MsgBox(ex.Message) Finally myCn.Close() '關(guān)閉連接 End Try
在甘泉等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì) 網(wǎng)站設(shè)計(jì)制作定制網(wǎng)站建設(shè),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),成都全網(wǎng)營(yíng)銷推廣,外貿(mào)營(yíng)銷網(wǎng)站建設(shè),甘泉網(wǎng)站建設(shè)費(fèi)用合理。
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 = "當(dāng)前時(shí)間:" Date.Now
End Sub
Private Sub Enter_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Enter_Button.Click
'定義一個(gè)OLEDB連接字符串
Dim conStr As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=F:\test\test.mdb"
'實(shí)例化OLEDB連接
Dim con As OleDbConnection = New OleDbConnection(conStr)
Dim sql As New System.Text.StringBuilder
'定義數(shù)據(jù)庫(kù)插入語(yǔ)句
sql.Append("insert into Time_billing([Car_Num],[Enter_Time])")
sql.Append("values('" Trim(Car_Num_Text.Text) "','" Date.Now "')")
'打開(kāi)數(shù)據(jù)庫(kù)鏈接
con.Open()
'定義執(zhí)行命令
Dim cmd As New System.Data.OleDb.OleDbCommand(sql.ToString, con)
'執(zhí)行命令
cmd.ExecuteNonQuery()
'關(guān)閉數(shù)據(jù)庫(kù)鏈接
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拼接過(guò)程中最后不需要有分號(hào)
'在賦值時(shí),讀取字段值時(shí)必須使用在數(shù)據(jù)庫(kù)客戶端查詢時(shí)顯示的字段名
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("木有數(shù)據(jù)!")
End If
Enter_Time_Text.Text = Enter_time
Leave_Time_Text.Text = Leave_time
'求時(shí)間差
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("結(jié)算完成!")
'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
1、 用The SQL Server .NET Data Provider連接數(shù)據(jù)庫(kù)
The SQL Server .NET Data Provider是利用SqlConnection類來(lái)連接SQL Server7.0或更高版本的數(shù)據(jù)庫(kù),
SqlConnection類位于名稱空間System.Data.SqlClient下。
連接代碼:
Dim sqlConnection1 As SqlClient.SqlConnection
Dim strConnect As String=”data source=服務(wù)器名;initial catalog=數(shù)據(jù)庫(kù)名;user id=sa;password=;”
sqlConnection1=New System.Data.SqlClient.SqlConnection(strConnect)
sqlConnection1.open ‘打開(kāi)數(shù)據(jù)庫(kù)
sqlConnection1.close ‘關(guān)閉連接,釋放資源
2、 用The OLE DB .NET Data Provider連接數(shù)據(jù)庫(kù)
上面已經(jīng)說(shuō)過(guò),利用The OLE DB .NET Data Provider可以訪問(wèn)Access、Oracle和SQL Server等種數(shù)據(jù)
庫(kù),那么,它是怎樣訪問(wèn)這些數(shù)據(jù)庫(kù)的呢?The OLE DB .NET Data Provider是通過(guò)位于名稱空間Sy
stem.Data.OleDb類庫(kù)下的OleDbConnection類來(lái)連接這三種不同類型的數(shù)據(jù)庫(kù)的。下面舉例說(shuō)明:
1)連接SQL Server數(shù)據(jù)庫(kù)
Dim oleDbConnection1 As OleDb.OleDbConnection
Dim strConnect As Sting=”P(pán)rovider=SQLOLEDB;Persist Security Info=False;Data Source=服務(wù)器名;Initial Catalog=數(shù)據(jù)庫(kù)名;User ID=sa;Password=;”
oleDbConnection1=New System.Data.OleDb.OleDbConnection(strConnect)
2)連接Access數(shù)據(jù)庫(kù)
假設(shè)要連接的Access數(shù)據(jù)庫(kù)名為“Example.mdb”,存放在d:\Data\目錄下。
Dim oleDbConnection1 As OleDb.OleDbConnection
Dim strConnect As Sting=”P(pán)rovider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\Data\ Example.mdb”
oleDbConnection1= New System.Data.OleDb.OleDbConnection(strConnect)
3)連接Oracle數(shù)據(jù)庫(kù)
Dim oleDbConnection1 As OleDb.OleDbConnection
Dim strConnect As Sting=”P(pán)rovider=MSDAORA;Data Source=服務(wù)器名;User ID=用戶ID;Password=密碼;”
oleDbConnection1= New System.Data.OleDb.OleDbConnection(strConnect)
3、 用The ODBC .NET Data Provider連接數(shù)據(jù)庫(kù)
The ODBC .NET Data Provider連接數(shù)據(jù)庫(kù)是通過(guò)OdbcConnection類來(lái)實(shí)現(xiàn)的,這個(gè)類位于名稱空間
Microsoft.Data.Odbc下,而名稱空間Microsoft.Data.Odbc是封裝在Microsoft.Data.Odbc.dll文件下的。
由于篇幅有限,這里就只介紹連接Sql Server和Oracle數(shù)據(jù)庫(kù)的方法,其他數(shù)據(jù)庫(kù)的連接方法基本類
似,我就不再多講了。
1)連接Sql Server數(shù)據(jù)庫(kù)
Dim odbcDbConnetion1 As Microsoft.Data.OdbcConnection
Dim strConnect As Sting=”Driver={SQL Server};Server=服務(wù)器名;Uid=sa;pwd=;Database= 數(shù)據(jù)庫(kù)名;”
odbcDbConnetion1=New Microsoft.Data.OdbcConnection(strConnect)
2)連接Oracle數(shù)據(jù)庫(kù)
Dim odbcDbConnetion1 As Microsoft.Data.OdbcConnection
Dim strConnect As Sting=”Driver={Microsoft ODBC for Oracle};Server=服務(wù)器名;Uid=sa;pwd=;”
odbcDbConnetion1=New Microsoft.Data.OdbcConnection(strConnect)
四、總結(jié)
通過(guò)本文的介紹,讀者基本掌握了在Visual Basic.NET中用ADO.NET和ODBC.NET連接各種數(shù)據(jù)庫(kù)的方法
。以上三種驅(qū)動(dòng)針對(duì)不同的數(shù)據(jù)庫(kù),它們的性能方面也有很大的不同:The SQL Server .NET Data Provider
的效率最高;The OLE DB .NET Data Provider的效率比較底;The ODBC .NET Data Provider的效率最慢。
Private Sub Button _Click(ByVal sender As System Object ByVal e As System EventArgs) Handles Button Click 備份數(shù)據(jù)庫(kù) 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
恢復(fù) 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()) 殺死進(jìn)程
str = kill mydr( spid ) ToString() cm CommandText = str cm CommandType = CommandType Text Application DoEvents() cm ExecuteNonQuery() 殺進(jìn)程 End While mydr Close() cm = New SqlCommand( RESTORE DATABASE JXbase FROM DISK= C:\Backup cn) 恢復(fù)備份 cm ExecuteNonQuery() MsgBox( 恢復(fù)成功成功 ) cn Close() cn Close()
lishixinzhi/Article/program/net/201311/12901
下面這段代碼,是我用來(lái)計(jì)算每個(gè)月存500元進(jìn)銀行,連續(xù)30年,最后連本帶利能有多少錢(qián)。這里面涉及復(fù)利計(jì)算。界面中右邊的文本框用來(lái)輸出每一次計(jì)算的結(jié)果。
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 '大復(fù)利
? Dim fuli_small As Long '小復(fù)利
? Dim i As Integer '循環(huán)變量
? Dim DATAstring As String '數(shù)據(jù)字符串
? 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
? 結(jié)果_TextBox.Text = Str(fuli_big) + "元"
End Sub
大概的過(guò)程:
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()