C#如何將查詢到的數(shù)據(jù)庫(kù)里面的數(shù)據(jù)輸出到textbox控件
更新時(shí)間:2023年07月12日 10:28:20 作者:不想學(xué)習(xí)只想玩
這篇文章主要介紹了C#如何將查詢到的數(shù)據(jù)庫(kù)里面的數(shù)據(jù)輸出到textbox控件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
C#將查詢到的數(shù)據(jù)庫(kù)里面的數(shù)據(jù)輸出到textbox控件
C#連接數(shù)據(jù)庫(kù)(類(lèi)的形式)
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#將查詢到的數(shù)據(jù)庫(kù)里面的數(shù)據(jù)輸出到textbox控件
this.Text = "本單位信息--系統(tǒng)維護(hù)";
DataCon link = new DataCon();//類(lèi)的內(nèi)容見(jiàn)博客上面的鏈接
string sql = "select * from message";
//為指定的command對(duì)象執(zhí)行DataReader
SqlDataReader read = link.query(sql);//執(zhí)行sql語(yǔ)句
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; //處理負(fù)數(shù)
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#將控制臺(tái)輸出重定向到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) //參數(shù)必須是char,否則不會(huì)進(jìn)入
{
_textBox.Invoke(new Action(() =>
{
_textBox.AppendText(value.ToString());
}));
}
}
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C# http系列之以form-data方式上傳多個(gè)文件及鍵值對(duì)集合到遠(yuǎn)程服務(wù)器
這篇文章主要介紹了C# http系列之以form-data方式上傳多個(gè)文件及鍵值對(duì)集合到遠(yuǎn)程服務(wù)器,需要的朋友可以參考下2019-08-08
C#實(shí)現(xiàn)讀取Excel文件并將數(shù)據(jù)寫(xiě)入數(shù)據(jù)庫(kù)和DataTable
Excel文件是存儲(chǔ)表格數(shù)據(jù)的普遍格式,因此能夠高效地讀取和提取信息對(duì)于我們來(lái)說(shuō)至關(guān)重要,下面我們就來(lái)看看C#如何實(shí)現(xiàn)讀取Excel文件并將數(shù)據(jù)寫(xiě)入數(shù)據(jù)庫(kù)和DataTable吧2024-03-03
c#中SAPI使用總結(jié)——SpVoice的使用方法
最近使用C#重做了點(diǎn)名系統(tǒng)(要用到TTS,讓計(jì)算機(jī)點(diǎn)名)使用了SAPI,在這里總結(jié)一下SpVoice的使用方法。2011-10-10
C#實(shí)現(xiàn)判斷一個(gè)時(shí)間點(diǎn)是否位于給定時(shí)間區(qū)間的方法
這篇文章主要介紹了C#實(shí)現(xiàn)判斷一個(gè)時(shí)間點(diǎn)是否位于給定時(shí)間區(qū)間的方法,涉及C#針對(duì)時(shí)間的轉(zhuǎn)換與判定相關(guān)技巧,需要的朋友可以參考下2015-08-08

