【问题标题】:How do I display database contents onto c# textbox如何在 c# 文本框中显示数据库内容
【发布时间】:2013-11-11 17:08:28
【问题描述】:

背景 我正在将值从文本文件传输到 sqlite 数据库。这些值存储在列表中,然后传输到数据库中(下)

我想要什么 现在我想从数据库中获取值并将它们输出到应用程序中的文本框。基本上,用户应该点击一个按钮,上面写着“显示文件”,它应该只是将数据库中的所有列值显示到 c# 文本框中,本质上我只是重新创建数据库(在文本框中)。

问题: 我根本不知道 sqlite 或 sql 语法(新程序员)。我知道我必须使用 select 运行查询,但是随后如何选择所有列,然后在文本框中显示所有列?

到目前为止我的尝试:(可能有一些明显的错误)

 sqlite_cmd.CommandText = "SELECT * FROM abc";

 //Command object gives us a datareader object
 sqlite_datareader = sqlite_cmd.ExecuteReader();

 private void textBox1_TextChanged_1(object sender, EventArgs e)
 {
    // SQlite datareader allows us to run through the lines
    // while ( sqlite_datareader.Read())
    {

     // Below is my approach on trying to display all column values
     // textbox1.Text = sqlite_datareader["Seq", "Field "," Desc "," Len ", etc etc ]; 

    }

 }

}

旁注: 由于数据库的内容很大,而且文本框的大小有限,如果我能得到一些水平和垂直的滚动条就太好了

【问题讨论】:

  • 某种网格不是显示数据库内容的更好地方吗?
  • 既然您是编程新手,也许您应该研究一下文本框和文本区域之间的区别。

标签: c# sql sqlite


【解决方案1】:
use datagrid instead of textbox

//Con = Connection Object
//Create Command Object
string sql;
SqlDataAdapter da;
DataTable dt;
SqlCommand com = con.createCommand();

//abc = your table name
//Provide Statement to Command Object
sql = " SELECT * FROM abc";
//Create New Datatable to fill with data
dt = new DataTable();

//Create DataAdapter to fill data in DataTable via Adapter
da = new SqlDataAdapter(sql, con);
da.Fill(dt);

//gridView1 = Your GridView Name
gridView1.DataSource = dt;
gridView1.Refresh();

【讨论】:

    【解决方案2】:
    private void FillMyTextbox( )
    {
        // open your database connection
        var sqlLiteConnection = OpenYourConnection( );
    
        // create command
        using( var sqlLiteCommand = sqlLiteConnection.CreateCommand( ) )
        {
            sqlLiteCommand.CommandText = "select * from abc";
    
            // create the reader
            using( var sqlLiteReader = sqlLiteCommand.ExecuteReader( ) )
            {
                // a StringBuilder to store the contents of your table 
                var allRecords = new StringBuilder( );
    
                while( sqlLiteReader.Read( ) )
                {
                    for( int i = 0; i < sqlLiteReader.FieldCount; i++ )
                    {
                        // either adress the column via an index or its name
                        var columnContentAsString = sqlLiteReader[ i ].ToString( );
                        allRecords.Append( columnContentAsString );
                    }
                    allRecords.Append( Environment.NewLine );
                }
    
                textBox1.Text = allRecords.ToString( );
            }
        }
    
        // somehow close/dispose your connection
    }
    

    我希望这段代码能让您了解如何访问您的数据表。 using 语句将负责处理阅读器和命令对象。 我建议使用DataGridView 来显示您的数据,而不是使用TextBox,但也许出于测试目的它可以。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-01
      • 2016-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多