0

I'm briefly new into Blazor. I was wondering if it's possible to make a very basic connection to mysql database server hosted remotely via simple function:

string connectionString = "data source = database ip here,1600;Initial Catalog=HDSERVER;Persist Security Info=True; User ID=user_id; Password=Password()";

    int idUser = -1;

    UserModel User = new UserModel();

    public class UserModel
    {
        public string name { get; set; }
        public string pin { get; set; }
    }

    private void LoginUser()
    {
        Console.WriteLine(User.name);
        Console.WriteLine(User.pin);
        {
            try
            {
                Console.WriteLine("starting sql connection...");

                SqlConnection c = new SqlConnection(connectionString);

                Console.WriteLine("opening..");

                c.Open();

                Console.WriteLine("sql connection open..");

                SqlCommand cmd = new SqlCommand("Login", c);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@pin", pinCode);

                Console.WriteLine("getting data");

                var pId = cmd.Parameters.AddWithValue("@idUser", idUser);

                Console.WriteLine("pId: ", pId);

                pId.Direction = ParameterDirection.Output;

                cmd.ExecuteNonQuery();
                c.Close();

                Console.WriteLine("sql connection closed..");

                int id = (int)pId.Value;

                Console.WriteLine("id: ", id);

                if (id > 0)
                {
                    NavigationManager.NavigateTo("/home");
                }
                else
                {
                    Console.WriteLine("Cannot sing in!");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: ");
                Console.WriteLine(e.Message);
            }
        }
    }

Or maybe there is another specific way for doing that? I usually typing code in js & react, so for me everything here is so new and a little confusing me. It would be great as well if someone could share any tips how to do such things or from where getting a knowledge about Blazor !

Thanks in advance!

11
  • I don't think you can make a connection from the client but you could do it from the server which, even if it worked client-side, is the only way to be able to secure data access Commented Mar 19, 2021 at 17:02
  • Your WASM is fully send to the client! So any connection string you put in there could be seen by the client. Don't send sentitive data to the client. (The usual way to solve this is have an API run on a server that handles the DB example) Commented Mar 19, 2021 at 17:18
  • More links from this answer: "Call a web API from ASP.NET Core Blazor" and "Create a web API with ASP.NET Core" Commented Mar 19, 2021 at 17:28
  • @JHBonarius I'm totally aware of that, but in purpose of testing this, I'm doing that this way. This application will be hosted and specific data will be coded before sending it.The only thing is the way of connecting to database this way ;) Commented Mar 19, 2021 at 18:31
  • 3
    You cannot use SqlConnection from webassembly. You cannot expect all APIs to work in a browser. Commented Mar 19, 2021 at 19:13

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.