C# Binding db query to array of string's

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kurt Jakobsen
    New Member
    • Jun 2007
    • 38

    C# Binding db query to array of string's

    Hello,
    I have a MySQL query that I want to perform in a utility (not page) class. This query should fill up an array of string's and then return the array to the calling class. The array will then be base for new query's.
    But I am not able to bind the result of a query to an string [] sArr variable while system array does not contain a definition for DataSource.

    Is there anybody who can tell me if it is possible, and if so, how to fill up the string array with the result from the query without binding to page specific controls?

    This is my class that does not work:

    public static void GetNameInWOS(In t32 iWosID, string[] sNameArr)
    {
    string sRet="", strSQL="";
    string myConnString = ConfigurationSe ttings.AppSetti ngs["DSN_Fyrr"];
    MySqlConnection myConnection = new MySqlConnection (myConnString);
    strSQL = "SELECT Name "
    + "FROM NameTypeDef"
    + "WHERE NameId = " + iWosID
    + " ORDER BY Name;";
    MySqlDataAdapte r myDataAdapter = new MySqlDataAdapte r(strSQL, myConnection);
    DataSet myDataSet = new DataSet();
    myDataAdapter.F ill(myDataSet, "Name");
    sNameArr.DataSo urce = myDataSet;
    sNameArr.DataBi nd();
    myConnection.Cl ose();
    }

    best regards
    Kurt Jakobsen
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    Originally posted by Kurt Jakobsen
    Hello,
    I have a MySQL query that I want to perform in a utility (not page) class. This query should fill up an array of string's and then return the array to the calling class. The array will then be base for new query's.
    But I am not able to bind the result of a query to an string [] sArr variable while system array does not contain a definition for DataSource.

    Is there anybody who can tell me if it is possible, and if so, how to fill up the string array with the result from the query without binding to page specific controls?

    This is my class that does not work:

    public static void GetNameInWOS(In t32 iWosID, string[] sNameArr)
    {
    string sRet="", strSQL="";
    string myConnString = ConfigurationSe ttings.AppSetti ngs["DSN_Fyrr"];
    MySqlConnection myConnection = new MySqlConnection (myConnString);
    strSQL = "SELECT Name "
    + "FROM NameTypeDef"
    + "WHERE NameId = " + iWosID
    + " ORDER BY Name;";
    MySqlDataAdapte r myDataAdapter = new MySqlDataAdapte r(strSQL, myConnection);
    DataSet myDataSet = new DataSet();
    myDataAdapter.F ill(myDataSet, "Name");
    sNameArr.DataSo urce = myDataSet;
    sNameArr.DataBi nd();
    myConnection.Cl ose();
    }

    best regards
    Kurt Jakobsen
    Have you tried looping through each row of your dataset and adding a new item to an arraylist?

    Code:
    ArrayList list = new ArrayList();
    foreach(DataRow row in myDataSet.Tables["Name"].Rows)
    {
      list.Add(row["Name"].ToString();
    }
    Nathan

    Comment

    Working...