我尚未在统一答案论坛中收到此帖子的任何反馈,因此我在这里发帖以扩大知名度。
似乎这个问题已经在统一论坛中被问过几次,但没有解决我的问题的答案。我正在运行 unity 5.3.5f,SQL Server 2008 R2 Express(正在运行 SQL Server 2016,但读到 mono dll 仅与 SQL Server 2008 兼容)。我的资产文件夹中有正确的单声道 dll,并在我的项目中正确引用和构建。我在本地运行数据库。我可以使用 SQL Server Management Studio 很好地进入它,并且 Visual Studio 2015 也可以很好地连接到它。我已经完全放弃了防火墙。现在我只是想让它在统一思想中工作。我还验证了 SQL Server Management Studio 中允许传入连接,并且在 SQL Server 配置管理器中启用了 tcp/ip。
此外,据我所见,检查统一手册没有关于数据库连接的文档。
我的代码:
public string RetrieveTestData(string query) { string sqlConnectionString = "Server=zzzz\\SQLEXPRESS2008;" + "Database=mydb;" + "User ID=sa;" + "Password=mypass;";
Debug.Log(sqlConnectionString);
SqlConnection con = new SqlConnection(sqlConnectionString);
try
{
con.Open(); // error thrown here
SqlCommand cmd = new SqlCommand(query, con);
var fubar = cmd.ExecuteScalar();
con.Close();
return fubar.ToString();
}
catch (Exception ex)
{
Debug.Log(ex.StackTrace);
throw;
}
public void TestQuery()
{
Debug.Log(RetrieveTestData("select top 1 * from dbo.Weapon"));
}
我的错误:
SocketException:一个现有的连接被远程主机强行关闭。
堆栈跟踪:
在 System.Net.Sockets.Socket.ReceiveFrom_nochecks_exc (System.Byte [] buf,Int32 偏移量,Int32 大小,SocketFlags 标志,System.Net.EndPoint& remote_end,布尔 throwOnError,System.Int32& 错误)[0x00000] in :0 在系统.Net.Sockets.Socket.ReceiveFrom_nochecks (System.Byte[] buf, Int32 offset, Int32 size, SocketFlags flags, System.Net.EndPoint& remote_end) [0x00000] in :0 at System.Net.Sockets.Socket.ReceiveFrom (System .Byte[] 缓冲区,System.Net.EndPoint&remoteEP) [0x00000] 在 System.Net.Sockets.UdpClient.Receive (System.Net.IPEndPoint&remoteEP) [0x00000] 在 System.Net.Sockets.UdpClient.Receive (System.Net.IPEndPoint&remoteEP) [0x00000] 在 System.Data.SqlClient 的:0。 SqlConnection+SqlMonitorSocket.DiscoverTcpPort (Int32 timeoutSeconds) [0x00000] 在 System.Data.SqlClient.SqlConnection.DiscoverTcpPortViaSqlMonitor (System.String ServerName, System.String InstanceName) [0x00000] 在 System.Data.SqlClient.SqlConnection.DiscoverTcpPortViaSqlMonitor (System.String ServerName, System.String InstanceName) [0x00000] 在系统:0Data.SqlClient.SqlConnection.ParseDataSource (System.String theDataSource, System.Int32& thePort, System.String& theServerName) [0x00000] in :0 在 System.Data.SqlClient.SqlConnection.Open () [0x00000] in :0 at (wrapper remoting-invoke-with-check) System.Data.SqlClient.SqlConnection:Open () at Assets.Scripts.Data.DBBase.RetrieveTestData (System.String query) [0x00013] in C:\Development\Unity\twistedshadowsgame\TwistedShadows\ Assets\Scripts\Data\DBBase.cs:23 UnityEngine.Debug:Log(Object) Assets.Scripts.Data.DBBase:RetrieveTestData(String) (在 Assets/Scripts/Data/DBBase.cs:35) Assets.Scripts.Data .DBBase:TestQuery() (在 Assets/Scripts/Data/DBBase.cs:43) Assets.Scripts.Data.DBBase:Start() (在 Assets/Scripts/Data/DBBase.cs:48)theServerName) [0x00000] in :0 at System.Data.SqlClient.SqlConnection.Open () [0x00000] in :0 at (wrapper remoting-invoke-with-check) System.Data.SqlClient.SqlConnection:Open () at Assets C:\Development\Unity\twistedshadowsgame\TwistedShadows\Assets\Scripts\Data\DBBase.cs:23 UnityEngine.Debug:Log(Object) 资源中的 .Scripts.Data.DBBase.RetrieveTestData(System.String 查询)[0x00013]。 Scripts.Data.DBBase:RetrieveTestData(String) (at Assets/Scripts/Data/DBBase.cs:35) Assets.Scripts.Data.DBBase:TestQuery() (at Assets/Scripts/Data/DBBase.cs:43) .Scripts.Data.DBBase:Start()(位于 Assets/Scripts/Data/DBBase.cs:48)theServerName) [0x00000] in :0 at System.Data.SqlClient.SqlConnection.Open () [0x00000] in :0 at (wrapper remoting-invoke-with-check) System.Data.SqlClient.SqlConnection:Open () at Assets C:\Development\Unity\twistedshadowsgame\TwistedShadows\Assets\Scripts\Data\DBBase.cs:23 UnityEngine.Debug:Log(Object) 资源中的 .Scripts.Data.DBBase.RetrieveTestData(System.String 查询)[0x00013]。 Scripts.Data.DBBase:RetrieveTestData(String) (at Assets/Scripts/Data/DBBase.cs:35) Assets.Scripts.Data.DBBase:TestQuery() (at Assets/Scripts/Data/DBBase.cs:43) .Scripts.Data.DBBase:Start()(位于 Assets/Scripts/Data/DBBase.cs:48)字符串查询)[0x00013] 在 C:\Development\Unity\twistedshadowsgame\TwistedShadows\Assets\Scripts\Data\DBBase.cs:23 UnityEngine.Debug:Log(Object) Assets.Scripts.Data.DBBase:RetrieveTestData(String) (在 Assets/Scripts/Data/DBBase.cs:35) Assets.Scripts.Data.DBBase:TestQuery() (在 Assets/Scripts/Data/DBBase.cs:43) Assets.Scripts.Data.DBBase:Start() (在资产/脚本/数据/DBBase.cs:48)字符串查询)[0x00013] 在 C:\Development\Unity\twistedshadowsgame\TwistedShadows\Assets\Scripts\Data\DBBase.cs:23 UnityEngine.Debug:Log(Object) Assets.Scripts.Data.DBBase:RetrieveTestData(String) (在 Assets/Scripts/Data/DBBase.cs:35) Assets.Scripts.Data.DBBase:TestQuery() (在 Assets/Scripts/Data/DBBase.cs:43) Assets.Scripts.Data.DBBase:Start() (在资产/脚本/数据/DBBase.cs:48)
非常感谢所有帮助,因为这是阻碍我所有进步的单一项目。如果我没有发布任何东西或者我需要在其他地方发布,请告诉我。我宁愿不必为此设置单独的休息服务。我相信统一有能力,从我读到的内容来看,它看起来已经被许多其他人成功地完成了。
提前致谢!瑞安