C# 如何连接其他计算机上的MySQL数据库

C# 如何连接其他计算机上的MySQL数据库,第1张

你好

1、mysql服务器,防火墙打开3306端口;

2、看看你的mysql是否设置允许其他机器连接了。

1打mysql中默认mysql数据库

2更改use表中的host键值(即将localhost---->%);

登录到管理器,添加一个用户,主机写%即可。

1)连接Oracle 8/8i/9i/10g/11g(thin模式)

ClassforName("oracleJDBCdriverOracleDriver")newInstance();

String url="JDBC:oracle:thin:@localhost:1521:orcl" //orcl为Oracle数据库的SID

String user="test";

String password="test";

Connection con=DriverManagergetConnection(url,user,password);

2)连接DB2数据库

ClassforName("comibmdb2jccDB2Driver");

String url="JDBC:db2://localhost:5000/testDb";/数据库连接串/

String user="test"; String password="test";

Connection con=DriverManagergetConnection(url,user,password);

3)连接MySQL数据库

ClassforName("commysqljdbcDriver");

String url="JDBC:mysql://localhost:8080/testDB";

String user="test"; String password="test";

Connection con=DriverManagergetConnection(url,user,password);

4)连接SQL Server数据库

ClassforName("commicrosoftJDBCsqlserverSQLServerDriver");

String url="JDBC:microsoft:sqlserver://localhost:1433;DatabaseName=testDb";

String user="test"; String password="test";

Connection con=DriverManagergetConnection(url,user,password);

5)连接PostgreSQL数据库

ClassforName("orgpostgresqlDriver");

String url="JDBC:postgresql://localhost/testDb";

String user="test"; String password="test";

Connection con=DriverManagergetConnection(url,user,password);

6)连接Access数据库

ClassforName("sunjdbcodbcJdbcOdbcDriver");

String url="JDBC:odbc:Driver={Microsoft Access Driver (mdb)};DBQ="+applicationgetRealPath("/Data/testDb/mdb");

Connection conn=DriverManagergetConnection(url,"","");

7连接Sybase数据库

ClassforName("comsybaseJDBCSybDriver");

String url="JDBC:sybase:Tds:localhost:5007/testDb";

Properties pro=SystemgetProperties();

proput("user","userId");

proput("password","user_password");

Connection con=DriverManagergetConnection(url,pro);

8连接informix数据库

ClassforName("cominformixJDBCifxDriver");

String url="JDBC:informix-sqli:localhost:1533/testDb:INFORMIXSERVER=myserver"user=testUser;password=testpassword"; Connection con=DriverManagergetConnection(url);

示例:

连接SQL Server2008R2数据库

首先Build Path → 添加外部sqljdbcjar驱动

import javasql;

public class DB {

public static void main(String[] args) throws Exception {

ClassforName("commicrosoftsqlserverjdbcSQLServerDriver");

Connection conn = DriverManagergetConnection("jdbc:sqlserver://localhost:1433; DatabaseName=数据库名", "sa", "1234");

Statement stmt = conncreateStatement();

ResultSet rs = stmtexecuteQuery("select from 表名");

while(rsnext()) {

Systemoutprintln("id为:" + rsgetString("id") + "name为:" + rsgetString("name"));

}

Systemoutprintln("数据库连接成功!");

rsclose();

stmtclose();

connclose();

Systemoutprintln("数据库成功关闭!");

}

}

以上就是关于C# 如何连接其他计算机上的MySQL数据库全部的内容,包括:C# 如何连接其他计算机上的MySQL数据库、mysql是怎么连接数据库服务器、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10156016.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存