简体   繁体   中英

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.

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. I have installed the MYSQL connector I need for visual studios and have connected to the data base. 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:

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
  • 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.

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.

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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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