如何通过单击按钮使用C#代码连接MySQL for VB.NET表单 [英] How to connect for MySQL for VB.NET form with C# Code By Click an Button

查看:105
本文介绍了如何通过单击按钮使用C#代码连接MySQL for VB.NET表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用C#代码为VB.NET表单的MySQL连接,方法是单击一个按钮,我使用了多个代码,但vb2008和sql server之间没有建立连接,请帮助我

How to connect for MySQL for VB.NET form with C# Code By Click an Button and i used several codes but no connection is establishing between the vb2008 and sql server please help me

推荐答案

首先,请确定您使用的是哪个系统:MySql或Sql Server-它们是不同的,并且需要稍微不同的处理.

大致轮廓是相同的.

首先,尝试使用服务器资源管理器"窗格在VS中建立连接:
1)打开服务器资源管理器.
2)右键单击数据连接",然后选择添加连接"
3)在随后的对话框中,选择您的数据源和数据库,指定安全信息,然后按测试连接"按钮.
4)连接正常后,按确定"
5)在服务器资源管理器"窗格中突出显示您的数据库,然后查看属性"窗格.将显示一个连接字符串的工作示例,您可以将其复制并粘贴到您的应用程序或配置文件中.

现在,您可以使用该字符串连接到数据库.
First, make up you mind which system you are using: MySql or Sql Server - they are different and require slightly different handling.

The broad outline is the same though.

First, try setting up a connection in VS with the Server Explorer pane:
1) Open Server Explorer.
2) Right click "Data connections" and select "Add connection"
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK"
5) highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file.

You now can use that string to connect to your database.
Using con As New SqlConnection(strConnect)
	con.Open()
	Using com As New SqlCommand("SELECT iD, description FROM myTable", con)
		Using reader As SqlDataReader = com.ExecuteReader()
			While reader.Read()
				Dim id__1 As Integer = CInt(reader("iD"))
				Dim desc As String = DirectCast(reader("description"), String)
				Console.WriteLine("ID: {0}" & vbLf & "    {1}", iD, desc)
			End While
		End Using
	End Using
End Using

对于MySql,请改用MySqlConnection,MySqlCommand和MySqlDataReader.

For MySql, use MySqlConnection, MySqlCommand, and MySqlDataReader instead.


这篇关于如何通过单击按钮使用C#代码连接MySQL for VB.NET表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆