简体   繁体   English

与实时数据库asp.net C#的连接字符串

[英]Connection string to a live database asp.net C#

I have set up a small test data base (local) using MS SQL Server, code bellow. 我已经使用MS SQL Server(代码如下)建立了一个小型测试数据库(本地)。

Code here 在这里编码

SqlConnection cs = new SqlConnection(@"Data Source = .\SQLEXPRESS; Initial Catalog = OMS; Integrated Security = true");
        SqlDataAdapter da = new SqlDataAdapter ();
        da.InsertCommand = new SqlCommand("INSERT INTO Customer(FirstName,LastName) VALUES (@FirstName,@LastName)", cs);
        da.InsertCommand.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = firstname.Text;
        da.InsertCommand.Parameters.Add("@LastName", SqlDbType.VarChar).Value = lastname.Text;

This is working well, now I have set up a MYSQL database hosted on a live server. 这运行良好,现在我已经在实时服务器上建立了一个MYSQL数据库。 I have installed the MYSQL connector I need for visual studios and have connected to the data base. 我已经安装了Visual Studio所需的MYSQL连接器,并已连接到数据库。 what I now wish to know is how to alter my connection string to pace the data into the live database. 我现在想知道的是如何更改我的连接字符串以将数据调整到实时数据库中。 I tried to put my host IP in eg: 我试图将主机IP放在例如:

SqlConnection cs = new SqlConnection(@"Data Source = 000.000.00.000; Initial Catalog = database name; Integrated Security = true");

However this is not working, any idea on what code I should use to connect to a live server? 但是,这不起作用,对于我应该使用什么代码连接到实时服务器的任何想法? any help would be appreciated. 任何帮助,将不胜感激。

The answer to your question is mostly related to the settings of the server and/or database. 您问题的答案主要与服务器和/或数据库的设置有关。 Make sure: 确保:

  • the ports on server are opened for remote communication. 服务器上的端口已打开以进行远程通信。
  • the application runs (as service) 应用程序运行(作为服务)
  • check the database configuration: mostly ports-wise, but you have to make sure there is SQL-login allowed 检查数据库配置:多数情况下是基于端口的,但必须确保允许SQL登录
  • create an user account inside the database and grant that account permissions to your database instance and to the proper table and to actions you want him to perform, some databases require granting him remote login rights, too 在数据库内部创建一个用户帐户,然后将该帐户权限授予您的数据库实例,适当的表以及您希望他执行的操作,某些数据库也需要授予他远程登录权限

When all this is checked, the connection string should be altered to something like: 选中所有这些后,连接字符串应更改为以下内容:

"Data Source=123.456.789.012;Initial Catalog=testdb; password=password; user id=login;"

This works fine for me, to connect with MSSQL2012 remote server database, since you are mixing MySQL and MSSQL, there could occur some other MySQL specific options. 对于我来说,与MSSQL2012远程服务器数据库连接起来效果很好,因为您正在混合使用MySQL和MSSQL,因此可能会出现其他一些特定于MySQL的选项。

EDIT: from what I see you did in comments to other answer, CHECK TWICE, how you handle the port -> you are using a comma in the ip string, whereas i hope you should avoid using it or use colon. 编辑:从我看到您在评论中所做的到其他答案,请检查两次,如何处理端口->您在ip字符串中使用逗号,而我希望您避免使用它或使用冒号。

all working thanks to some great advice. 都得益于一些很好的建议 working code for reference 工作代码供参考

{
        MySqlConnection cs = new MySqlConnection(@"Data Source = 000.000.00.000;username=*******;password=******; Initial Catalog = database; Integrated Security = true");
       MySqlDataAdapter da = new MySqlDataAdapter ();
        da.InsertCommand = new MySqlCommand("INSERT INTO Customer(FirstName,LastName) VALUES (@FirstName,@LastName)", cs);
        da.InsertCommand.Parameters.Add("@FirstName", MySqlDbType.VarChar).Value = firstname.Text;
        da.InsertCommand.Parameters.Add("@LastName", MySqlDbType.VarChar).Value = lastname.Text;

        cs.Open();
        da.InsertCommand.ExecuteNonQuery(); 
        cs.Close();
    }

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

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