Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends here I attach my coding , c# with SQLite database connectivity , in this I am trying to store the table entries to string variable, I cant do it, now I get the one row in dataset, it has 3 entries in one row how to store that 3 entries in three string variables, i want your help

C#
public void DbCon()
        {
			string term1,term2,term3;
            string dataBasePath = @"D:\MyProjects\DbCon.db3";
            DataSet ds = new DataSet();
            using (SQLiteConnection connection = new SQLiteConnection(this._CONNECTION_STRING, dataBasePath)))
            {
                try
                {
                    if (connection.State == ConnectionState.Closed) connection.Open();
                    SQLiteDataAdapter adapterLang = new SQLiteDataAdapter("SELECT * FROM AminoAcid where Term1='Alanine' AND Term2='Ala' AND Term3='1'", connection);
                    adapterLang.Fill(ds);
                    object[] rowvals = new object[3];
                }
                finally
                {
                    if (connection.State == System.Data.ConnectionState.Open) connection.Close();
                }
            }
Posted

1 solution

OK - a DataSet is a Collection of DataTables. In this case, a single table (but do check):
C#
if (ds.Tables.Count > 0)
   {
   DataTable dt = ds.Tables[0];
   ...
And a DataTable is a collection of DataRows. In this case, it should be a single row, but again, check first.
C#
if (dt.Rows.Count > 0)
   {
   DataRow dr = dt.Rows[0];
   ...
And a DataRow is a collection of cells. The data you want in is the cells.
There are two ways to access those: by index or by Column Name. Since you don't specify the columns you want from your table (and you should) I can't fill it all in for you, but...all you then have to do is cast it to the right data type:
C#
if (ds.Tables.Count > 0)
    {
    DataTable dt = ds.Tables[0];
    if (dt.Rows.Count > 0)
        {
        DataRow dr = dt.Rows[0];
        string str1 = (string)dr[0];
        string str2 = (string)dr[1];
        string str3 = (string)dr[2];
        //...
        }
    }
Or (better)
C#
if (ds.Tables.Count > 0)
    {
    DataTable dt = ds.Tables[0];
    if (dt.Rows.Count > 0)
        {
        DataRow dr = dt.Rows[0];
        string str1 = (string)dr["Column1 name"];
        string str2 = (string)dr["Column2 name"];
        string str3 = (string)dr["Column3 name"];
        //...
        }
    }
Using names, and listing the columns you want to return is better practice, since it means your code doesn't crash if the underlying table is changed, and it doesn't return unnecessary columns which can seriously slow things down.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900