简体   繁体   English

C# - CS1503 - 参数 1:无法从“字符串”转换为“整数”

[英]C# - CS1503 - Argument 1: cannot convert from 'string' to 'int'

Before you reply, I've checked my code over & over again and have also searched for about an hour now for a similar answer.在你回复之前,我一遍又一遍地检查了我的代码,现在也搜索了大约一个小时来寻找类似的答案。 The compiler keeps throwing the error CS1503, I'm not really sure how to fix this.编译器不断抛出错误 CS1503,我不确定如何解决这个问题。 It's on lines 36 and 37 and I've commented the lines that have the error, 36 and 37. It's for a database that is supposed to search for the license plate and output the other data in the table.它位于第 36 行和第 37 行,我已经注释了有错误的行,即第 36 行和第 37 行。它用于应该搜索车牌并在表中输出其他数据的数据库。

public partial class Login : Form
{
    string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=J:\Plate Reader\DB\InfoDB.mdf;Integrated Security=True;Connect Timeout=30";
    SqlDataReader mdr;

    public Login()
    {
        InitializeComponent();
    }

    private void btnSearch_Click(object sender, EventArgs e)
    {
        using (SqlConnection sqlCon = new SqlConnection(connectionString))
        {
            sqlCon.Open();
            string selectQuery = "SELECT * From Plate WHERE Plate='" + txtPlate.Text;
            SqlCommand command = new SqlCommand(selectQuery, sqlCon);

            mdr = command.ExecuteReader();

            if (mdr.Read())
            {
                labelName.Text = mdr.GetString("Name");  //Right here
                labelWanted.Text = mdr.GetInt32("Bounty").ToString(); //and here
            }
            else
            {
                MessageBox.Show("No Data For This Plate");
            }
        }
    }
}

As explained in comments, GetString as well GetInt32 requires a parameter of type integer.正如注释中所解释的,GetString 和 GetInt32 都需要一个整数类型的参数。 That integer is the position of the field in the Select field list.该整数是该字段在“选择字段”列表中的位置。 If you do not want to use a position you could write如果你不想使用一个职位,你可以写

 labelName.Text = mdr.GetString(mdr.GetOrdinal("Name")); 

and this simple line could be easily transformed in an extension method adding a method to a static class whose code is并且这个简单的行可以很容易地转换为扩展方法,将方法添加到静态类,其代码为

public static class ReaderExtensions
{
     public static string GetString(this SqlDataReader source, string fieldName)
     {
         return source.GetString(source.GetOrdinal(fieldName));
     }
}

and this finally allows you to write这最终允许你写

string labelText = mdr.GetString("Name");

of course the same could be written also for a GetInt32 that accepts a field name.当然,对于接受字段名称的 GetInt32 也可以编写相同的内容。 By the way, if I am not mistaken the MySql version has these overloads directly in the assembly顺便说一句,如果我没记错的话,MySql 版本在程序集中直接有这些重载

GetInt32 is expecting an 'int' parameter. GetInt32需要一个“int”参数。

You are passing in a string.您正在传递一个字符串。

You need to send in the ordinal position of your columns.您需要发送的顺序位置

use this instead:改用这个:

mdr.GetInt32(mdr.GetOrdinal("Name"));

You might want to consider using a micro ORM like Dapper to make things easier.您可能需要考虑使用像 Dapper 这样的微型 ORM 来使事情变得更容易。

The reason is that GetString(i) and GetInt32(i) methods requires zero-based column ordinal as an parameter.原因是GetString(i)GetInt32(i)方法需要从零开始的列序号作为参数。 If you want to retrieve data from the column by name you should use indexer :如果要按名称从列中检索数据,则应使用indexer

labelName.Text = mdr["Name"].ToString();  
labelWanted.Text = mdr["Bounty"].ToString();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CS1503:参数 1:无法从 'int' 转换为 'byte' C# - CS1503: Argument 1: cannot convert from 'int' to 'byte' C# C# 错误 CS1503 参数 1:无法从“字符串”转换为“字符” - C# Error CS1503 Argument 1: cannot convert from 'string' to 'Character' CS1503 C# 参数 1:无法从“字符串”转换为“System.Data.SqlClient.SqlCommand” - CS1503 C# Argument 1: cannot convert from 'string' to 'System.Data.SqlClient.SqlCommand' CS1503 参数 1:无法从 'string' 转换为 'string[*,*]' - CS1503 Argument1: cannot convert from 'string' to 'string[*,*]' C#无法从void转换为bool - CS1503 - C# Cannot convert from void to bool - CS1503 CS1503 C#参数1:无法从“ System.IO.Stream”转换为“ char []” - CS1503 C# Argument 1: cannot convert from 'System.IO.Stream' to 'char[]' CS1503:参数 2:无法从 &#39;System.Action[*,*,*] 转换为 System.Action[]&#39; C# 2022 - CS1503: Argument 2: Cannot convert from 'System.Action[*,*,*] to System.Action[]' C# 2022 出现错误 CS1503:“参数 1:无法从 'System.Diagnostics,PerformanceCounter' 转换为 'int' - Getting Error CS1503: "Argument 1: Cannot convert from 'System.Diagnostics,PerformanceCounter' to 'int' CS1503 C#参数1:无法从“ Project.Vector2”转换为“ System.Predicate” <Project.Vector2> ” - CS1503 C# Argument 1: cannot convert from “Project.Vector2” to “System.Predicate<Project.Vector2>” CS1503:参数 3:无法从“double”转换为“UnityEngine.Quaternion”帮助! 统一二维 c# - CS1503: Argument 3: cannot convert from 'double' to 'UnityEngine.Quaternion' HELP! unity 2D c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM