如何将表条目存储在sqlite中的字符串变量中? [英] How to store the table entries in string variable in sqlite?

查看:152
本文介绍了如何将表条目存储在sqlite中的字符串变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友在这里我附上我的编码,c#与SQLite数据库连接,在这我试图将表条目存储到字符串变量,我不能这样做,现在我得到了一行数据集,它在一行中有3个条目如何在3个字符串变量中存储3个条目,我想要你的帮助



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

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();
                }
            }

推荐答案

确定 - DataSet是DataTables的集合。在这种情况下,单个表(但要检查):

OK - a DataSet is a Collection of DataTables. In this case, a single table (but do check):
if (ds.Tables.Count > 0)
   {
   DataTable dt = ds.Tables[0];
   ...

DataTable是DataRows的集合。在这种情况下,它应该是一行,但是再次检查。

And a DataTable is a collection of DataRows. In this case, it should be a single row, but again, check first.

if (dt.Rows.Count > 0)
   {
   DataRow dr = dt.Rows[0];
   ...

DataRow是一组单元格。您想要的数据是单元格。

有两种方法可以访问它们:按索引或按列名称。既然你没有从你的表中指定你想要的列(你应该),我无法为你填写全部内容,但是......你所要做的就是将它转换为正确的数据类型:

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:

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)

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.


这篇关于如何将表条目存储在sqlite中的字符串变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆