c# - 如何与 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();
}
}```
解决方案
在 Catch 中输入以下内容并查看您朋友机器上的错误,当您查看他机器上的事件查看器时,它会告诉您确切的错误是什么。
using (EventLog eventLog = new EventLog("Application"))
{
eventLog.Source = "SQL Error: From My Application";
eventLog.WriteEntry(ex.StackTrace, EventLogEntryType.Error, 101, 1);
}
推荐阅读
- python - 努力让这个 GUI 工作并给我一个可以选择图像的组合框界面
- react-native - React Native:带有 flex 的 ScrollView 和布局元素
- angular - 我可以在 mat-select 中发出不同的值吗?
- postgresql - 容器化数据库文件路径和根目录未知
- asp.net-core - 运行时基于对象属性值的动态 XML 元素名称
- c++ - 如何在 char* 中添加 char*?
- r - 如何合并两个数据集一列将在两列之间?
- airflow - 在 apache 气流 2.0 中可以避免这个警告吗?
- reporting-services - SSRS - 在文本框中连接多个参数值
- windows - 什么可能会使系统挂起到鼠标和键盘无响应的地步