C#如何将查询到的数据库里面的数据输出到textbox控件

 更新时间:2023年07月12日 10:28:20   作者:不想学习只想玩  
这篇文章主要介绍了C#如何将查询到的数据库里面的数据输出到textbox控件问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

C#将查询到的数据库里面的数据输出到textbox控件

C#连接数据库(类的形式)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace systemprotect
{
    public partial class bdwxx : Form
    {
        public bdwxx()
        {
            InitializeComponent();
        }
        private void bdwxx_Load(object sender, EventArgs e)
        {//C#将查询到的数据库里面的数据输出到textbox控件
            this.Text = "本单位信息--系统维护";
            DataCon link = new DataCon();//类的内容见博客上面的链接
            string sql = "select * from message";
            //为指定的command对象执行DataReader
            SqlDataReader read = link.query(sql);//执行sql语句
            while (read.Read())
            {
                Console.WriteLine("{0} {1} ", read["b_id"],read["b_name"]);
                textBox1.Text = read["b_id"].ToString();//b_id字段名,下同
                textBox2.Text = read["b_name"].ToString();
                textBox3.Text = read["b_tel"].ToString();
                textBox4.Text = read["b_person"].ToString();
                textBox5.Text = read["b_adress"].ToString();
            }
            link.close();
            Console.ReadLine();
            textBox1.ReadOnly = true;
            textBox2.ReadOnly = true;
            textBox3.ReadOnly = true;
            textBox4.ReadOnly = true;
            textBox5.ReadOnly = true;
        }
        private void s(object sender, EventArgs e)
        {
        }
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            Formmainxtwh1 form = new Formmainxtwh1();
            form.Show();
            this.Hide();
        }
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            textBox1.ReadOnly = false;
            textBox2.ReadOnly = false;
            textBox3.ReadOnly = false;
            textBox4.ReadOnly = false;
            textBox5.ReadOnly = false;
        }
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            string id = textBox1.Text;
            string name = textBox2.Text;
            string tel = textBox3.Text;
            string person = textBox4.Text;
            string address = textBox5.Text;
            DataCon link = new DataCon();
            string update = "Update message set b_id='" + id + "',b_name='"+name+ "',b_tel='"+tel+ "',b_person='" + person + "',b_adress='" + address + "'";
            link.insert(update);
            link.close();
        }
        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 0x20) e.KeyChar = (char)0;  //禁止空格键
            if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return;   //处理负数
            if (e.KeyChar > 0x20)
            {
                try
                {
                    double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
                }
                catch
                {
                    e.KeyChar = (char)0;   //处理非法字符
                }
            }
        }
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
        }
        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            Formmainxtwh1 form = new Formmainxtwh1();
            form.Show();
            this.Hide();
        }
    }
}

C#将控制台输出重定向到TextBox控件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UIWinformTest
{
    public partial class FormConsoleOutput : Form
    {
        public FormConsoleOutput()
        {
            InitializeComponent();
        }
        private void FormConsoleOutput_Load(object sender, EventArgs e)
        {
            Console.SetOut(new ConsoleTextWriter(textBox1));
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Random r = new Random((int)DateTime.Now.Ticks);
            Console.WriteLine("Random Integer Output:\r\n" + r.Next());
        }
    }
    public class ConsoleTextWriter : TextWriter
    {
        private TextBox _textBox;
        public override Encoding Encoding => Encoding.UTF8;
        public ConsoleTextWriter(TextBox textBox)
        {
            _textBox = textBox;
        }
        public override void Write(char value) //参数必须是char,否则不会进入
        {
            _textBox.Invoke(new Action(() =>
            {
                _textBox.AppendText(value.ToString());
            }));
        }
    }
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • c# 免费组件html转pdf的实现过程

    c# 免费组件html转pdf的实现过程

    这篇文章主要介绍了c# 免费组件html转pdf的实现过程,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • 用c#实现简易的计算器功能实例代码

    用c#实现简易的计算器功能实例代码

    这篇文章主要介绍了c#实现简易的计算器功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • C#中Abstract 、Virtual和Override的使用及区别

    C#中Abstract 、Virtual和Override的使用及区别

    C#中virtual,abstract,override用于方法重载,子类覆盖了父类的相同方法,父类中的实现不可能再被外面调用。本文给大家重点介绍C#中Abstract 、Virtual和Override的使用及区别,需要的朋友参考下吧
    2021-06-06
  • C# 对XML基本操作代码总结

    C# 对XML基本操作代码总结

    C# 对XML基本操作包括读取节点的数据,添加节点。读取节点属性,修改节点属性等
    2011-10-10
  • C# winform跨线程操作控件的实现

    C# winform跨线程操作控件的实现

    本文主要介绍了C# winform跨线程操作控件的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • C#特性之匿名方法和Lambda表达式

    C#特性之匿名方法和Lambda表达式

    这篇文章主要介绍了C#特性之匿名方法和Lambda表达式,需要的朋友可以参考下
    2014-12-12
  • c#编写webservice服务引用实例分享

    c#编写webservice服务引用实例分享

    c#编写webservice服务引用实例分享,大家参考使用吧
    2013-12-12
  • C# wpf 无边框窗口添加阴影效果的实现

    C# wpf 无边框窗口添加阴影效果的实现

    在本篇内容中小编给大家整理了一篇关于C# wpf 无边框窗口添加阴影效果的具体方法内容,有兴趣的朋友们可以学习参考下
    2022-11-11
  • C#格式化json字符串的方法分析

    C#格式化json字符串的方法分析

    这篇文章主要介绍了C#格式化json字符串的方法,结合实例形式分析了C#针对json字符串格式化的原理、步骤与具体实现技巧,需要的朋友可以参考下
    2017-06-06
  • C#对象与XMl文件之间的相互转换

    C#对象与XMl文件之间的相互转换

    本文是对C#中对象与XMl文件之间的相互转换进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助
    2013-10-10

最新评论