Vb.net 欢迎如果数据库没有通过本地网络连接到数据库,您能给我一个代码给我一个错误消息吗
欢迎光临。你能给我一个代码,如果它没有通过本地网络连接到数据库,会给我一个错误消息吗。 注意:该程序将通过Visual Basic创建一个连接到本地网络上数据库的帐户Vb.net 欢迎如果数据库没有通过本地网络连接到数据库,您能给我一个代码给我一个错误消息吗,vb.net,Vb.net,欢迎光临。你能给我一个代码,如果它没有通过本地网络连接到数据库,会给我一个错误消息吗。 注意:该程序将通过Visual Basic创建一个连接到本地网络上数据库的帐户 Dim con As New SqlConnection("Data source=Younis-PC\SQLEXPRESS,1433;Initial catalog=account; User ID=sa; Password=12345678;") Dim da As New SqlDataAdapter Di
Dim con As New SqlConnection("Data source=Younis-PC\SQLEXPRESS,1433;Initial catalog=account; User ID=sa; Password=12345678;")
Dim da As New SqlDataAdapter
Dim dt As New DataTable
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If TextBox1.Text = "" Or TextBox2.Text = "" Then
MsgBox("Make sure that the user name or password is not empty")
Return
End If
da = New SqlDataAdapter("SELECT * FROM Table_1 where username = '" & TextBox1.Text & "'", con)
da.Fill(dt)
If dt.Rows.Count <> 0 Then
MessageBox.Show("This user name is already used by Please use another name")
Else
Dim cmd As New SqlCommand("INSERT INTO Table_1 (username, passwords) values ('" & TextBox1.Text & "','" & TextBox2.Text & "')", con)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
TextBox1.Text = ""
TextBox2.Text = ""
Me.Hide()
acuser.Show()
MsgBox("Account created")
End If
End Sub
Dim con作为新的SqlConnection(“数据源=Younis PC\SQLEXPRESS,1433;初始目录=帐户;用户ID=sa;密码=12345678;”)
Dim da作为新的SqlDataAdapter
Dim dt作为新数据表
私有子按钮2\u单击(发送者作为对象,e作为事件参数)处理按钮2。单击
如果TextBox1.Text=“”或TextBox2.Text=“”,则
MsgBox(“确保用户名或密码不为空”)
返回
如果结束
da=新的SqlDataAdapter(“从表_1中选择*,其中username='”&TextBox1.Text&“'”,con)
da.填充(dt)
如果dt.Rows.Count为0,则
MessageBox.Show(“此用户名已被使用,请使用其他名称”)
其他的
Dim cmd作为新的SqlCommand(“插入到表_1(用户名、密码)值(‘“&TextBox1.Text&’,‘“&TextBox2.Text&’)”,con)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
TextBox1.Text=“”
TextBox2.Text=“”
我躲起来
acuser.Show()
MsgBox(“创建的帐户”)
如果结束
端接头
将数据对象保留在本地,以便您可以正确关闭和处置它们。即使出现错误,使用块也可以实现这一点
使用If-Exists有几个优点
con.Open()
周围放了一个Try…Catch…End Try
。您还可以在Execute周围放置一个,以查看查询是否有效
在真正的应用程序中,您永远不会将密码存储为纯文本。请注意最近的Facebook丑闻
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text = "" Or TextBox2.Text = "" Then
MessageBox.Show("Make sure that the user name or password is not empty")
Return
End If
Dim Result As Integer
Dim query = "If Exists(Select 1 From Table_1 where username = @username)
Select 0 Else Select 1
Insert Table_1 (username, passwords) Values (@usename, @passwords);"
Using con As New SqlConnection("Data source=Younis-PC\SQLEXPRESS,1433;Initial catalog=account; User ID=sa; Password=12345678;")
Using cmd As New SqlCommand(query, con)
cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = TextBox1.Text
cmd.Parameters.Add("@passwords", SqlDbType.VarChar).Value = TextBox2.Text
Try
con.Open()
Catch ex As Exception
MessageBox.Show("Connection Error - " & ex.Message)
Return
End Try
Result = CInt(cmd.ExecuteScalar)
End Using
End Using
If Result = 0 Then
MessageBox.Show("This user name is already used by Please use another name")
Else
MessageBox.Show("Account created")
TextBox1.Text = ""
TextBox2.Text = ""
acuser.Show()
Me.Hide()
End If
End Sub
警告:您的代码易受SQL注入攻击。您应该使用参数化查询和准备好的语句来帮助防止攻击者使用恶意输入值破坏您的数据库。给出了风险的解释,以及如何使用ADO.NET安全地编写查询的一些示例。切勿将未初始化的数据直接插入SQL。按照现在编写代码的方式,有人很容易窃取、错误更改甚至删除您的数据。无论如何,如果尝试连接失败,您的代码将抛出异常并崩溃。如果您想使该异常更加用户友好,那么捕获异常(使用try/catch),记录它(以便以后可以进行调查),然后向用户显示一个带有非技术性解释的消息框。如果需要,您还可以保持应用程序运行,并重新尝试连接(或给用户一个按钮),和/或告诉他们如果问题仍然存在,可以在哪里获得技术支持。