Как хранить данные в базе данных на основе услуг? - PullRequest
0 голосов
/ 09 июля 2019

Я пытаюсь сохранить имя пользователя и пароль в таблице с именем «Пользователь», которая находится в базе данных на основе сервиса.

Ниже приведен код того, что я пробовал.

private void Btn_register_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //Create the conection string and open the conn
                SqlConnection conne = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=\\CUC-SRV-FS02\Studio-StuHome$\13mihailovs.m\Documents\IT_Unit4\IT_Unit4\ITUnit4.mdf;Integrated Security=True");


                //Open the connection string
                conne.Open();

                //Get all the values from the text boxes etc and pass them over to the DB
                string insertQuery = "insert into User(Username, Password) " +
                    "values(@Username, @Password)";
                SqlCommand com = new SqlCommand(insertQuery, conne);

                //Get values from the controls such as the text boxes and pass them over to the DB
                com.Parameters.AddWithValue("@Username", txt_username.Text);
                com.Parameters.AddWithValue("@Password", txt_password.Text);

                //This actually executes the query with the given values above.
                com.ExecuteNonQuery();

                //Dispose the connection string once the data has been passed over the DB
               conne.Close();

            }
            catch (Exception problem)
            {
                MessageBox.Show("error has occured");
            }
        }

1 Ответ

0 голосов
/ 09 июля 2019

правильный путь для вставки в базу данных в Ado.Net:

    private readonly SqlConnection _con = new SqlConnection("Data Source=.;Initial 
    Catalog=dbPhoneBook;Integrated Security=True");

public string Add(string user , string pass)
    {
        string result = "";
        SqlCommand cmd = new SqlCommand();
        try
        {
            cmd.Connection = _con;
            cmd.CommandText = "insert into tbl_login(user,pass)values(@user,@pass)";
            cmd.Parameters.AddWithValue("@user", user);
            cmd.Parameters.AddWithValue("@pass", pass);
            if (_con.State != ConnectionState.Open)
            {
                _con.Open();
            }
            cmd.ExecuteNonQuery();
            result = "Ok";
            cmd.Dispose();
        }
        catch
        {
            cmd.Dispose();
            result = "NOk";

        }
        return result;
    }

Также проверьте следующее Проверьте веб-сайт https://www.connectionstrings.com/ ссылка на базу данных.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...