在 unity 上使用 c# 访问 MySQL 数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17029389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing MySQL database using c# on unity?
提问by Cody
I have just started using C# and do not know much about it. I am using the 3d game engine called Unity and trying to write a c# script to access the MySQL database that I am running. The MySQL database is on a different computer. My question is how can I access the MySQL database using C#. I know the username, password and database I want to use, but I can not figure out how to access the database.
我刚刚开始使用 C#,对它了解不多。我正在使用名为 Unity 的 3d 游戏引擎并尝试编写 ac# 脚本来访问我正在运行的 MySQL 数据库。MySQL 数据库位于另一台计算机上。我的问题是如何使用 C# 访问 MySQL 数据库。我知道我想使用的用户名、密码和数据库,但我不知道如何访问数据库。
回答by Adrian
You will need to get the C# MySQL driver: http://dev.mysql.com/downloads/connector/net/
您需要获取 C# MySQL 驱动程序:http: //dev.mysql.com/downloads/connector/net/
And then you will need to follow the MySQL manual for setting it up and using it. It's a standard ADO.NET driver, so you should be able to follow most any C# SQL tutorials out there for additional help.
然后你需要按照 MySQL 手册来设置和使用它。它是一个标准的 ADO.NET 驱动程序,因此您应该能够按照大多数 C# SQL 教程获得更多帮助。
回答by Yogee
Unity3D uses stripped Mono.net which doesn't support database directly. You can write your own DLL in C# .NET which does data access for you. Then add it in your Unity3D project.
Unity3D 使用不直接支持数据库的剥离 Mono.net。您可以在 C# .NET 中编写自己的 DLL,它为您执行数据访问。然后将其添加到您的 Unity3D 项目中。
While you are writing your own DLL, make sure when you bring that DLL to your project, you also copy referenced DLL in the same folder. Best approach would be to use Mono.NET.
在编写自己的 DLL 时,请确保在将该 DLL 引入项目时,还将引用的 DLL 复制到同一文件夹中。最好的方法是使用 Mono.NET。
回答by seckincengiz
A late answer, but maybe this will help others.
一个迟到的答案,但也许这会帮助其他人。
I can actually access my server side sql database on my non-game Unity app. In my case, my app controls whether the user serial number true or not. After users entered their serial key my app starts to compare the serial numbers on sql database. You can use this Server Side Highscores Tutorialfor your start point. I did the same. Follow these steps then you should be able to acces your sql database on Unity.
我实际上可以在我的非游戏 Unity 应用程序上访问我的服务器端 sql 数据库。就我而言,我的应用程序控制用户序列号是否为真。用户输入他们的序列号后,我的应用程序开始比较 sql 数据库上的序列号。您可以使用此服务器端高分教程作为起点。我也这样做了。按照以下步骤操作,您应该能够在 Unity 上访问您的 sql 数据库。
回答by Seyed Morteza Kamali
Database (SQLite) Setup for UnityCreate new folder
Unity 的数据库 (SQLite) 设置创建新文件夹
Create new folder under Assets Folder Rename it Plugins.
Copy sqlite3.def and sqlite3.dll into Assets/Plugins in your unity project .You can download these files here http://www.sqlite.org/download.htmlfor windows (Precompiled Binaries for Windows)
- Download SQLite Browser http://sourceforge.net/projects/sqlitebrowser/or http://sqliteadmin.orbmu2k.de/download SQLite Administrator tool
- Create Database in Assets folder in your unity project using SQLite Browser.
- Copy System.Data.dll and Mono.Data.Sqlite.dll from **C:\Program Files (x86)\Unity \Editor\Data\Mono\lib\mono\2.0* and paste them in your Assets/Plugins* folder in your unity project.
- Add these namespaces using Mono.Data.Sqlite; using System.Data; using System;
- string conn= "URI=file:" + Application.dataPath + "/PickAndPlaceDatabase.s3db";
在 Assets 文件夹下创建新文件夹,将其重命名为 Plugins。
将 sqlite3.def 和 sqlite3.dll 复制到您的统一项目中的 Assets/Plugins 中。您可以在这里下载这些文件http://www.sqlite.org/download.htmlfor windows (Precompiled Binaries for Windows)
- 下载 SQLite 浏览器http://sourceforge.net/projects/sqlitebrowser/或http://sqliteadmin.orbmu2k.de/下载 SQLite 管理员工具
- 使用 SQLite 浏览器在您的统一项目的 Assets 文件夹中创建数据库。
- 从 **C:\Program Files (x86)\Unity\Editor\Data\Mono\lib\mono\2.0* 复制 System.Data.dll 和 Mono.Data.Sqlite.dll 并将它们粘贴到您的 Assets/Plugins* 文件夹中在您的统一项目中。
- 使用 Mono.Data.Sqlite 添加这些命名空间;使用 System.Data; 使用系统;
- string conn="URI=file:" + Application.dataPath + "/PickAndPlaceDatabase.s3db";
Replace PickAndPlaceDatabase.s3db with your database name
将 PickAndPlaceDatabase.s3db 替换为您的数据库名称
under Assets Folder Rename it Plugins .
在 Assets 文件夹下重命名 Plugins 。
void Start () {
string conn = "URI=file:" + Application.dataPath + "/PickAndPlaceDatabase.s3db"; //Path to database.
IDbConnection dbconn;
dbconn = (IDbConnection) new SqliteConnection(conn);
dbconn.Open(); //Open connection to the database.
IDbCommand dbcmd = dbconn.CreateCommand();
string sqlQuery = "SELECT value,name, randomSequence " + "FROM PlaceSequence";
dbcmd.CommandText = sqlQuery;
IDataReader reader = dbcmd.ExecuteReader();
while (reader.Read())
{
int value = reader.GetInt32(0);
string name = reader.GetString(1);
int rand = reader.GetInt32(2);
Debug.Log( "value= "+value+" name ="+name+" random ="+ rand);
}
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbconn.Close();
dbconn = null;
}
these is useful link:
这些是有用的链接:
Further SQLite Help Visit : http://www.tutorialspoint.com/sqlite/
更多 SQLite 帮助访问:http: //www.tutorialspoint.com/sqlite/
http://wiki.unity3d.com/index.php/Webservices_In_Unity
then you should add this library into your C# code :
http://wiki.unity3d.com/index.php/Webservices_In_Unity
那么你应该把这个库添加到你的 C# 代码中:
using AssemblyCSharp.portal.wwwww.com;
now you can use it:
现在你可以使用它:
using UnityEngine;
using System.Collections;
using AssemblyCSharp.portal.wwwww.com;
public class Game5_Player : MonoBehaviour
{
public string Logo;
void Start ()
{
crm1 s = new crm1();
Logo= s.getCompanyLogo ();
}
}
回答by u5675325
You can find a procedure in Unity Wikiwith PHP, MySQL and C#/JavaScript.
您可以在Unity Wiki 中使用 PHP、MySQL 和 C#/JavaScript找到一个过程。
It consists of three steps
它由三个步骤组成
- Create a blank MySQL Database and a table.
- Create a PHP server side script (This will connect to the MySQL table, receive data from a Unity script (step 3), and query the database (the examples given are either inserting data or selecting))
- Create the Unity Controller Script (This will connect to the PHP script created in step 2)
- 创建一个空白的 MySQL 数据库和一个表。
- 创建一个 PHP 服务器端脚本(这将连接到 MySQL 表,从 Unity 脚本接收数据(步骤 3),并查询数据库(给出的示例是插入数据或选择))
- 创建 Unity 控制器脚本(这将连接到第 2 步中创建的 PHP 脚本)
回答by RemiGc
I have faced the same problem yesterday and I've found a satisfying solution that works both on PC and Android.
我昨天遇到了同样的问题,我找到了一个令人满意的解决方案,它适用于 PC 和 Android。
1: Download .DLL file from here matching your Visual studio project target .NET version (for me 3.5, version 6.9.8.0 worked just fine). If you download a wrong version you will get an error in Unity editor. links to download the file: https://www.dllme.com/dll/files/mysql_data_dll.htmlor this one: https://downloads.mysql.com/archives/c-net/
1:从这里下载与您的 Visual Studio 项目目标 .NET 版本匹配的 .DLL 文件(对我来说 3.5,版本 6.9.8.0 工作得很好)。如果您下载了错误的版本,您将在 Unity 编辑器中收到错误消息。下载文件的链接:https: //www.dllme.com/dll/files/mysql_data_dll.html或这个:https: //downloads.mysql.com/archives/c-net/
2: Unpack the .DLL file and include it in the project (put it anywhere inside of the Assets folder).
2:解压 .DLL 文件并将其包含在项目中(将其放在 Assets 文件夹内的任何位置)。
3: Program your connection to the database ;) here is a short example:
3:对与数据库的连接进行编程;) 这是一个简短的示例:
using System;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;
public class Tutorial4
{
public static void Main()
{
string connStr = "server=localhost;user=root;database=world;port=3306;password=******";
MySqlConnection conn = new MySqlConnection(connStr);
try
{
Console.WriteLine("Connecting to MySQL...");
conn.Open();
string sql = "SELECT COUNT(*) FROM Country";
MySqlCommand cmd = new MySqlCommand(sql, conn);
object result = cmd.ExecuteScalar();
if (result != null)
{
int r = Convert.ToInt32(result);
Console.WriteLine("Number of countries in the world database is: " + r);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
conn.Close();
Console.WriteLine("Done.");
}
}

