How connect to sql database | server | not local | not use web service, api

Nort ⠀ 671 Reputation points
2021-03-22T19:07:17.363+00:00

I have quite a bit of experience developing for Windows, but not so long ago I switched to Xamarin.
I need to connect to a Microsoft SQL database, without using web services and api, the database is not local, it is on a public server.
I need any help!

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,301 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,895 questions
0 comments No comments
{count} vote

Accepted answer
  1. Nort ⠀ 671 Reputation points
    2021-04-22T19:36:38.24+00:00

    Here's what I needed:

      public static ObservableCollection<Goods> List { get; set; }
        private const string stringConnection = @"Data Source = 111.111.11.11; Initial Catalog = Market; User Id = Ivan; Password = 1234567890";
        private const string sqlQuery = "uSp_NameOfStoredProcedure";
        private static void GetGoods()
                {
                    List = new ObservableCollection<Goods>();
                    using (SqlConnection con = new SqlConnection(stringConnection))
                    {
                        con.Open();
                        using (SqlCommand command = new SqlCommand(sqlQuery, con))
                        {
                            command.CommandType = CommandType.StoredProcedure;
                            using (SqlDataReader reader = command.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    Goods g = new Goods()
                                    {
                                        ID = reader.GetInt64(0),
                                        Name = reader.GetString(1),
                                        Price = reader.GetString(2),
                                    };
                                    List.Add(g);
                                }
                            }
                        }
                    }
                }
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,706 Reputation points Microsoft Vendor
    2021-03-23T04:18:17.807+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    If I understand correctly, you can use SQL Server Management Studio (SSMS) to connect and query a SQL Server instance.

    The steps :

    1.Start SQL Server Management Studio. The first time you run SSMS, the Connect to Server window opens. If it doesn't open, you can open it manually by selecting Object Explorer > Connect > Database Engine.

    2.The Connect to Server dialog box appears. Enter the necessary information.

    3.After you've completed all the fields, select Connect.

    4.To verify that your SQL Server connection succeeded, expand and explore the objects within Object Explorer where the server name, the SQL Server version, and the username are displayed. These objects are different depending on the server type.

    For more details, you can check: https://learn.microsoft.com/en-us/sql/ssms/quickstarts/ssms-connect-query-sql-server?view=sql-server-ver15

    Best Regards,

    Jessie Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.