MessageBox.Show();报最匹配的重载方法具有一些无效参数解决方法

初学者在使用Ado.net连接数据库时遇到MessageBox.Show()报错,问题在于.NET Framework版本不同导致的Show方法参数类型不匹配。在.NET 4.5中,Show方法可以接受int,而在.NET 4.0中仅接受string。解决方案是在调用Show方法时确保传递的是字符串,例如通过变量.ToString()转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我是一个刚刚学习Ado.net的新手,今天在连接数据库的过程中出现了MessageBox.Show();报最匹配的重载方法具有一些无效参数。因为我是抄传智播客老师的代码,老师的不报错误,而我的确偏偏报这个错误,查不出来原因。于是我请教了一个大神,经他指点发现了原因所在。

报错原因:最匹配的重载方法具有一些无效参数。

解决方法:老师装的是.net framework 4.5类库,而我只装了4.0的类库;4.5的show方法默认可以接收int类型的值,而4.0的show方法只能接收string类型的值,所以老师不报错,而我的报错。我的Show方法只能接收String Text 的值,于是我在变量后面加了个.ToString()方法错误解决了。

下面就是代码,这是一个窗体应用程序的button点击事件

 private void button6_Click(object sender, EventArgs e)

        {

//创建数据库的连接通道,但通道还没有修好

            SqlConnection conn = new SqlConnection("server=.;database=jkk;uid=datahealth;pwd=zxcv%1234%asdf");

//准备一个命令对象

            SqlCommand cmd = conn.CreateCommand();

//告诉命令对象要做的事情,就是Sql语句

            cmd.CommandText = "select count(id) from dbo.users";

//打开数据库连接通道

            conn.Open();

//定义一个int类型的变量来接收对象执行后传回来的参数

            int res = Convert.ToInt32(cmd.ExecuteScalar());

//打印对象执行后传回来的参数

            MessageBox.Show(res.ToString());

        }

这个问题的解决思路是:看Show需要什么参数,就在括号中传入什么参数,参数不是它想要的程序就会报错。

namespace _2 { public partial class Form1 : Form { // 添加公共属性存储登录用户名和用户ID public string LoggedInUsername { get; private set; } = string.Empty; public int LoggedInUserId { get; private set; } = -1; // 数据库连接字符串(建议提取到配置文件中) private readonly string connectionString = "Data Source=LAPTOP-265KA3NF;Initial Catalog=note;Integrated Security=True;"; public Form1() { InitializeComponent(); } // 打开修改密码窗口 public void OpenUpdatePasswordForm() { if (string.IsNullOrEmpty(LoggedInUsername)) { MessageBox.Show("请先登录"); return; } FormUpdatePwd_User updateForm = new FormUpdatePwd_User(LoggedInUserId); updateForm.ShowDialog(); } private void btnChangePswChar_Click(object sender, EventArgs e) { } private void username_TextChanged(object sender, EventArgs e) { } private void label2_Click(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { string account = txtid.Text; string password = this.password.Text; if (string.IsNullOrEmpty(account) || string.IsNullOrEmpty(password)) { MessageBox.Show("账号和密码不能为空!"); return; } bool isAdmin = rbtnAdmin.Checked; string tableName = isAdmin ? "T_Admin" : "T_User"; try { // 只调用一次验证方法 if (ValidateUserLogin(account, password, tableName)) { int userId = GetUserId(account, tableName); this.LoggedInUserId = userId; this.LoggedInUsername = account; string successMessage = isAdmin ? "管理员登录成功!" : "用户登录成功!"; MessageBox.Show(successMessage); if (isAdmin) { 管理员主菜单 adminForm = new 管理员主菜单(); adminForm.ShowDialog(); } else { // 使用userId打开Form2 Form2 userForm = new Form2(userId, account); userForm.ShowDialog(); } this.Hide(); } else { MessageBox.Show("账号或密码错误!"); } } catch (SqlException ex) { MessageBox.Show($"数据库连接错误:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { MessageBox.Show($"发生未知错误:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } // 获取用户ID private int GetUserId(string account, string table) { using (SqlConnection conn = new SqlConnection(connectionString)) { string query = $"SELECT Uid FROM [{table}] WHERE Uname = @Uname"; using (SqlCommand cmd = new SqlCommand(query, conn)) { cmd.Parameters.AddWithValue("@Uname", account); conn.Open(); object result = cmd.ExecuteScalar(); return result != null ? Convert.ToInt32(result) : -1; } } } // 验证用户登录信息 private bool ValidateUserLogin(string account, string password, string table) { using (SqlConnection conn = new SqlConnection(connectionString)) { string query = $"SELECT COUNT(1) FROM [{table}] WHERE Uname = @Uname AND Pwd = @Pwd"; using (SqlCommand cmd = new SqlCommand(query, conn)) { cmd.Parameters.AddWithValue("@Uname", account); cmd.Parameters.AddWithValue("@Pwd", password); conn.Open(); return (int)cmd.ExecuteScalar() > 0; } } } private void 注册_Click(object sender, EventArgs e) { 注册 registerForm = new 注册(); registerForm.ShowDialog(); } private void radioButton1_CheckedChanged(object sender, EventArgs e) { } private void password_TextChanged(object sender, EventArgs e) { } protected override void OnFormClosing(FormClosingEventArgs e) { base.OnFormClosing(e); if (e.CloseReason == CloseReason.UserClosing) { var result = MessageBox.Show("确定要退出程序吗?", "退出确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.No) { e.Cancel = true; } } } private void checkBox1_CheckedChanged(object sender, EventArgs e) { } } } 错:FormUpdatePwd_User updateForm = new FormUpdatePwd_User(LoggedInUserId);LoggedInUserId无法从int转换为string
07-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值