如何使用我的 C# windows 应用程序访问 Sql 服务器在线数据库

How to access Sql server online database with my C# windows application

我有 windows 使用 C# 的应用程序,我想将数据存储在本地 SQL SERVER 数据库以及在线 SQL SERVER 数据库中。

你不应该在这里问这样的问题,但这里有一些可以帮助你入门的东西。

using(SqlConnection cnn = new SqlConnection())
{
    cnn.ConnectionString="your connection string";

    // /*e.g.: */ 
    cnn.ConnectionString = "Server=123.123.123.123,1433;Database=DbName;User id=user;Password=pass;";

    using(SqlCommand cmd = new SqlCommand())
    {
        cmd.CommandType = CommandType.StoredProcedure; //or whatever it is
        cmd.ConnectionTimeout = 0; //or however long you want
        cmd.Connection = cnn;

        cmd.CommandText = "your query";
        //cmd.Parameters.AddWithValue("@SP_PARAM", object); if you are calling an SP

        cnn.Open();
        cmd.ExecuteNonQuery(); //whatever you are executing
        cnn.Close();

    }
}