错误8无法将类型'string []'隐式转换为'int []'

错误8无法将类型'string []'隐式转换为'int []'

问题描述:

大家好,这是基于输入的字符串进行搜索的,我正在重写代码,以便可以完全相同的方式进行搜索,但是基于整数,我试图将字符串的实例更改为"int"或"int64",但我知道它无法正常工作,值得一试,只是对如何使它读取"int ClaimID"而不是"string ClaimID"感到好奇
任何帮助将不胜感激!

Hi Guys, this does a search based on a string being input, I am rewriting the code so that i can have it do the search exactly the same way but based on an integer, i tried to change instances of string to "int" or "int64" but i know that it wouldnt work, was worth a try, just curious on how i can get it working to read "int claimID" instead of "string ClaimID"
any help would be greatly appreciated!

public string[] searchClaim(string ClaimID)
        {
            //Connect to DB
            OleDbConnection con = new OleDbConnection(connect);
            con.Open();
            try
            {
                //SQL command to slect the claim data and add to an array to pass back
                string sql = "SELECT * FROM Claim WHERE ClaimID =" + ClaimID + "";
                OleDbCommand command = new OleDbCommand(sql, con);
                OleDbDataReader reader = command.ExecuteReader();
                reader.Read();
                string[] dbResult = new String[5]; //error here when i change it to int64 instead of string
                dbResult[0] = "Success";
                dbResult[1] = reader[0].ToString(); //also errors here saying int cannot be converted to long
                dbResult[2] = reader[1].ToString(); 
                dbResult[3] = reader[2].ToString(); 
                dbResult[4] = reader[3].ToString(); 
                con.Close();
                return dbResult; //error here as well
            }
            catch
            {
                throw new Exception();
            }
            finally
            {
                // Close the connection
                if (con != null)
                {
                    con.Close();
                }
            }
        }

您可以通过许多不同的方式(读取数据)进行操作(例如,
1)对OleDbDataReader使用Read()方法: ^ ]
2)或使用DataTable.Load(OleDbDataReader)方法
http://msdn.microsoft.com/en-us/library/system. data.datatable.load.aspx [ ^ ]
3)或DataSet.Load()方法: http://msdn.microsoft.com/en-us/library/yab24tfx.aspx [ ^ ]

希望对您有帮助...

第二个想法[/EDIT]
如果您确实需要使用字符串数组,请阅读有关类型转换,尤其是显式转换的更多信息.
http://www.dotnetspider.com/resources/27313-implicit-explicit-conversion- c.aspx [ ^ ]
http://msdn.microsoft.com/en-us/library/ms173105.aspx [ ^ ]
http://social.msdn.microsoft.com/论坛/en-US/csharpgeneral/thread/808ad927-385f-49c9-aace-256eefe659d7 [ http://msdn.microsoft.com/en-us/library/ms173105%28v = vs.80%29.aspx [ ^ ]
You can do it (read data) in many different ways, for example,
1) using Read() method for OleDbDataReader: http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdatareader%28v=vs.80%29.aspx#Y0[^]
2) or using DataTable.Load(OleDbDataReader) method
http://msdn.microsoft.com/en-us/library/system.data.datatable.load.aspx[^]
3) or DataSet.Load() method: http://msdn.microsoft.com/en-us/library/yab24tfx.aspx[^]

I hope it will help...

SECOND THOUGHT[/EDIT]
If you really need to use array of string, please, read more about type conversion, especially about explicit conversion.
http://www.dotnetspider.com/resources/27313-implicit-explicit-conversion-c.aspx[^]
http://msdn.microsoft.com/en-us/library/ms173105.aspx[^]
http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/808ad927-385f-49c9-aace-256eefe659d7[^]
http://msdn.microsoft.com/en-us/library/ms173105%28v=vs.80%29.aspx[^]


要使此功能像您认为的那样起作用,是:
To get this to work like you think it would work is:
public int[] searchClaim(string ClaimID)
        {
            //Connect to DB
            OleDbConnection con = new OleDbConnection(connect);
            con.Open();
            try
            {
                //SQL command to slect the claim data and add to an array to pass back
                string sql = "SELECT * FROM Claim WHERE ClaimID =" + ClaimID + "";
                OleDbCommand command = new OleDbCommand(sql, con);
                OleDbDataReader reader = command.ExecuteReader();
                reader.Read();
                int[] dbResult = new int[5]; 
                dbResult[0] = 1;
                dbResult[1] = reader[0];
                dbResult[2] = reader[1]; 
                dbResult[3] = reader[2]; 
                dbResult[4] = reader[3]; 
                con.Close();
                return dbResult;             }
            catch
            {
                throw new Exception();
            }
            finally
            {
                // Close the connection
                if (con != null)
                {
                    con.Close();
                }
            }
        }



看来您正在从数据库中获取int数据类型,因此您需要一个int数组而不是Int64. int!= Int64&& int!=字符串

正如losmac正确指出的那样,只有在您的数据库模式设置为int数据类型的情况下,这种方式才能工作.如果您的数据类型混合在一起,则将无法使用.



It would appear that you are getting int datatypes from your database so you need an int array not an Int64. int != Int64 && int != string

As losmac rightfully pointed out, this would only work this way IF your database schema is setup with int datatypes. If your datatypes are mixed, this will not work.