首页 > 解决方案 > 如何与 SQL 数据库共享 c# 代码

问题描述

我和我的朋友正在使用 c#、WPF 和 Visual Studio 制作应用程序。我们使用 MS SQL Server 做了一个登录功能,数据库在我的电脑上。当我将源代码分享给我的朋友并且他在 Visual Studio(在他的 PC 上)中打开它时,他无法像我一样登录。只要他按下“登录”按钮,程序就会立即崩溃,甚至没有给出任何错误。

在我的 PC 上,当我单击“登录”按钮时,它成功移动到下一页,因此我们认为它与 SQL 数据库有关。我怎样才能使它也适用于他的 PC?

登录按钮代码

private void UserSignInBtn_Click(object sender, RoutedEventArgs e)
        {
            SqlConnection sqlCon = new SqlConnection(connectionString);


            try
            {
                if (sqlCon.State == ConnectionState.Closed)
                {
                    sqlCon.Open();
                    string query = "SELECT COUNT (1) FROM tblSignUP WHERE StudentName=@StudentName AND Password=@Password";
                    SqlCommand sqlCmd = new SqlCommand(query, sqlCon);
                    sqlCmd.CommandType = CommandType.Text;
                    sqlCmd.Parameters.AddWithValue("@StudentName", tbID.Text);
                    sqlCmd.Parameters.AddWithValue("@Password", PB.Password);
                    int count = Convert.ToInt32(sqlCmd.ExecuteScalar());
                    if (count == 1)
                    {

                        // Custom Message Box and Dim Effect
                        var jim = new Dim();

                        jim.Show();
                        this.Effect = new BlurEffect();

                        var lsmb = new Custom_MessageBoxes.LoginSuccessfulMsgBox();
                        lsmb.ShowDialog();

                        this.Effect = null;
                        jim.Close();

                        var User_Homepage = new User_Homepage();
                        NavigationService.Navigate(User_Homepage);
                    }
                    else
                    {
                       // Custom Message Box and Dim Effect 2
                        var him = new Dim();

                        him.Show();
                        this.Effect = new BlurEffect();

                        var rmdlgb = new ReturnMessageDialogueBox();
                        rmdlgb.ShowDialog();

                        this.Effect = null;
                        him.Close();

                    }
                }
            }
            catch(Exception ex)
            {

            }
            finally
            {
                sqlCon.Close();
            }
        }```

标签: c#sql-serverwpfxaml

解决方案


在 Catch 中输入以下内容并查看您朋友机器上的错误,当您查看他机器上的事件查看器时,它会告诉您确切的错误是什么。

        using (EventLog eventLog = new EventLog("Application"))
        {
            eventLog.Source = "SQL Error: From My Application";
            eventLog.WriteEntry(ex.StackTrace, EventLogEntryType.Error, 101, 1);
        }

推荐阅读