简体   繁体   English

如何连接到本地MSSQL数据库

[英]How do I connect to a Local MSSQL Database

EDIT: I've just tested it in ASP.NET and it works perfectly fine. 编辑:我刚刚在ASP.NET中对其进行了测试,它工作得很好。 So no issue with the connection string or anything. 因此,连接字符串等都没有问题。 Guess Unity doesn't like this method? 猜猜Unity不喜欢这种方法吗? Maybe there's some more DLL's I need to copy? 也许还有一些我需要复制的DLL? Any ideas? 有任何想法吗?

So I'm making a game in Unity and I'm trying to use the System.Data.SqlClient library to connect to some stored procedures I have made for things such as registering a user. 因此,我正在Unity中进行游戏,并且尝试使用System.Data.SqlClient库连接到我为诸如注册用户之类的事情而创建的一些存储过程。

I have copied the System.Data.dll from "C:\\Program Files\\Unity\\Editor\\Data\\Mono\\lib\\mono\\unity" and that has all worked fine. 我已经从“ C​​:\\ Program Files \\ Unity \\ Editor \\ Data \\ Mono \\ lib \\ mono \\ unity”复制了System.Data.dll,并且一切正常。

I'm currently using this connection string, which works fine on an ASP.NET application but just using a different mdf: 我当前正在使用此连接字符串,该字符串在ASP.NET应用程序上可以正常工作,但仅使用其他mdf:

private string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='C:\Users\uppy8\Desktop\Computer Science Project\Mining Game\Assets\MineRace.mdf';Integrated Security = True";

The problem occurs when running this code: 运行此代码时会发生问题:

using System.Data.SqlClient;
using System.IO;

public void Login()
{
    Crypto crypto = new Crypto();

    using (SqlConnection conn = new SqlConnection(connectionString))
    {
        try
        {
            conn.Open();
        } catch (Exception e)
        {
            Debug.Log(e);
        }

        SqlCommand command = new SqlCommand("USERS_LOGIN_USER", conn);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add(new SqlParameter("@Username", usernameInputField.text));

        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                if (crypto.EncryptString(passwordInputField.text) == reader["password"].ToString())
                {
                    UserAccountManager.instance.userInfo = FetchUserInfo((int)reader["id"]);
                }
            }
        }
    }

The problem happens on the line "conn.Open()", where Unity gives me the error: 问题发生在“ conn.Open()”行,Unity给我错误:

System.Net.Sockets.SocketException: No such host is known.

Furthermore, without the try catch, the error occurs where I create a new SqlDataReader, where I get this issue: 此外,如果没有try catch,则会在我创建新的SqlDataReader的地方发生错误,并在其中出现以下问题:

InvalidOperationException: ExecuteReader requires an open connection to continue. This connection is closed.

I understand that this is an issue with the connection, in that it's not running or the connection isn't working properly, however I can't seem to find a solution and I have a sneaky suspicion that it's something to do with Unity not supporting this library. 我知道这是连接的问题,因为它没有运行或连接无法正常工作,但是我似乎找不到解决方案,并且暗中怀疑它与Unity不支持有关这个图书馆。

Some more clarification just before I end off: 在我结束之前还要进行一些澄清:

  • The user enters their credentials into the "usernameInputField" and "passwordInputField" 用户将其凭据输入“ usernameInputField”和“ passwordInputField”
  • The user presses Login, which runs the "Login" method shown above 用户按下登录,运行上面显示的“登录”方法
  • The error occurs. 发生错误。

If any more information is required please leave a comment. 如果需要更多信息,请发表评论。

Thanks! 谢谢!

What is the scope of connectionString? connectionString的范围是什么? Do you need to pass the connectionString to the Login() function? 您是否需要将connectionString传递给Login()函数?

public void Login(string connectionString)

I am a DBA, not a .Net developer, so forgive me if my questions are too basic or if my .Net syntax is wrong. 我是DBA,而不是.Net开发人员,所以如果我的问题太基础或.Net语法错误,请原谅我。

You can try this 你可以试试这个

string connectionString = @"Data Source = (localdb)\MSSQLLocalDB; Initial Catalog ='C:\USERS\uppy8\Desktop\Computer Science Project\Mining Game\Assets\MineRace.mdf'; Integrated Security = True; Connect Timeout = 30; Encrypt = False; TrustServerCertificate = True; ApplicationIntent = ReadWrite; MultiSubnetFailover = False"
        SqlConnection con = new SqlConnection();   
                    if (con.State==ConnectionState.Open)
                    {
                        con.Close();
                        con.ConnectionString = connectionString;
                        con.Open();
                        cmd.Connection = con;
                    }
                    else
                    {
                        con.ConnectionString = connectionString;
                        con.Open();
                        cmd.Connection = con;
                    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM