如何将数据存储在基于服务的数据库中?

时间:2019-07-09 09:19:34

标签: c# database wpf

我正在尝试将用户名和密码存储在基于服务的数据库中的名为“ User”的表中。

下面是我尝试过的代码。

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 :(得分:0)

在Ado.Net中插入数据库的方法:

# Cravler\/axMindGeoIpBundle/src/Resources/config/services.xml
<services>
  <service
      id="Cravler\MaxMindGeoIpBundle\Command\UpdateDatabaseCommand" 
      class="Cravler\MaxMindGeoIpBundle\Command\UpdateDatabaseCommand" 
      public="false">
      <tag name="console.command" />
  </service>
</services>

还请检查以下内容 检出网站https://www.connectionstrings.com/到数据库的链接。