Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法将源类型“string”转换为目标类型“uint”,c中出现错误_C#_Mysql - Fatal编程技术网

C# 无法将源类型“string”转换为目标类型“uint”,c中出现错误

C# 无法将源类型“string”转换为目标类型“uint”,c中出现错误,c#,mysql,C#,Mysql,我正在VS2010中开发一个WinForms应用程序,用于从Sql Server或MySQL检索数据 在本文中,我使用下面的GUI分配连接钉 通过下面的函数添加或修改目的地的MySQL连接字符串 private static void CreateDConnectionString(string server,string port, string dataBase, string userId, string password) { try {

我正在VS2010中开发一个WinForms应用程序,用于从Sql Server或MySQL检索数据

在本文中,我使用下面的GUI分配连接钉

通过下面的函数添加或修改目的地的MySQL连接字符串

    private static void CreateDConnectionString(string server,string port, string dataBase, string userId, string password)
    {
        try
        {
            var connectionBuilder = new MySqlConnectionStringBuilder()
            {

                Server = server,
                Port = port,
                Database = dataBase,
                UserID = userId,
                Password = password
            };
            //Open the app.config for modification
            var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            //Retreive connection string setting
            var connectionString = config.ConnectionStrings.ConnectionStrings["DConnection"];
            if (connectionString == null)
            {
                //Create connection string if it doesn't exist
                config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings
                {
                    Name = "DConnection",
                    ConnectionString = connectionBuilder.ConnectionString,
                    ProviderName = "MySql.Data.MySqlClient"
                });
            }
            else
            {
                //Only modify the connection string if it does exist
                connectionString.ConnectionString = connectionBuilder.ConnectionString;
            }
            //Save changes in the app.config
            config.Save(ConfigurationSaveMode.Modified);
        }
        catch (Exception)
        {
            MessageBox.Show(@"Need to work on configration");
            throw;
        }
    }
在这个函数中,对代码进行修改

Port = port,
我收到无法将源类型字符串转换为目标类型uint的错误

您需要将字符串端口转换为端口的数据类型

Port  = Convert.ToUInt32(port, 16);
您需要将字符串端口转换为端口的数据类型

Port  = Convert.ToUInt32(port, 16);

端口不接受字符串值

它需要一个整数

因此,要么传递一个整数,要么使用Int32.Parseport

编辑:显然与文档相反,它是一个uInt

Uint32.Parse(port)

端口不接受字符串值

它需要一个整数

因此,要么传递一个整数,要么使用Int32.Parseport

编辑:显然与文档相反,它是一个uInt

Uint32.Parse(port)
由于MySqlConnectionStringBuilder.Port的类型为uint unsigned integer,并且您正在尝试分配字符串类型的对象:这些类型不兼容,因此它们之间没有隐式转换

您应该首先验证字符串是否可以转换为uint,如下所示:

编辑:修复了代码中的错误,因为MySqlConnectionStringBuilder.Port的类型为uint unsigned integer,并且您正在尝试分配字符串类型的对象:这些类型不兼容,它们之间没有隐式转换

您应该首先验证字符串是否可以转换为uint,如下所示:


编辑:修复了代码中的错误

尝试使用int现在显示“无法将源类型“int”转换为目标类型”unitUint在这种情况下是一个无符号整数使用UInt32。parsetrued使用int现在显示“无法将源类型“int”转换为目标类型”unitUint在这种情况下是一个无符号整数使用UInt32。请注意,它不是单位,这是乌因。它的意思是无符号整数只是一个音符,它不是单位,而是单位。它表示无符号整数