如何从SQL数据库访问/搜索数据并将其显示在带有文本框的表单中 [英] How can I access/search data from SQL database and display it in a form with textbox

查看:403
本文介绍了如何从SQL数据库访问/搜索数据并将其显示在带有文本框的表单中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我刚学习C#,目前我正在使用SQL数据库进行银行模拟。



问题:我仍然无法访问或显示将特定帐号的数据写入表格。



需要:当我在文本框中输入帐号并单击按钮时,新表单将显示并显示所有信息有这个帐号。



这是我的代码:

Hello everyone I'm just learning C# and currently I'm working with Bank Simulation using SQL database.

Problem: I still can't access or display the specific account number's data into a form.

Need: is when I input account number into text box and click a button a new form will show and display all the information that has this account number.

here is my code:

private void btnOpen_Click(object sender, EventArgs e)
{
    con = new System.Data.SqlServerCe.SqlCeConnection();
    con.ConnectionString = "Data Source=C:\\Users\\Lovette\\Documents\\MyFiles\\ETEEAP\\MODULE-1 C#\\Bankdatabase\\BankAccount.sdf";

    comd = new SqlCeCommand("SELECT * From tbl_bankaccount WHERE AccountNumber='" + tbAccountNum.Text.Trim() + "'", con);
    con.Open();

    reader = comd.ExecuteReader();

    clientRecordForm clientRecords = new clientRecordForm(this);

    while (reader.Read())
    {
        tbAccountNumPR.Text = reader.GetValue(0).ToString();
        tbFirstName.Text = reader.GetValue(1).ToString();
        tbMiddleName.Text = reader.GetValue(2).ToString();
        tbLastName.Text = reader.GetValue(3).ToString();
        tbEmployer.Text = reader.GetValue(4).ToString();
        tbBusinessAdd.Text = reader.GetValue(5).ToString();
        tbHomeAdd.Text = reader.GetValue(6).ToString();
        tbContactNum.Text = reader.GetValue(7).ToString();
        tbMoney.Text = reader.GetValue(8).ToString();
        clientRecords.ShowDialog();
    }
    con.Close();

推荐答案

试试这个



http://stackoverflow.com/questions/1346132/how-do-i-extract- data-from-a-datatable [ ^ ]


设计第一个带有txtbx和btn tp的windoe表格输入帐号

关于btn_click事件编写代码来调用现在将打开的第二个表单。



form2 obj = new form2();

obj.AccountNo = txtbx。文本; //这里AccountNo是form2中公开定义的字符串变量

obj.show();



现在编写查询以检索所有信息基于该AccountNo

并在不同的控件中显示它们。



说明: - 比dataReader更好地使用数据集。
design ur first windoe form with a txtbx and btn tp enter Account No.
On btn_click event write the code to call second form which will open now.

form2 obj = new form2();
obj.AccountNo = txtbx.text; // here AccountNo is a publicly defined string variable in form2
obj.show();

now write your query to retrieve all information based on that AccountNo
and show them in different controls.

Instruction:- Better to use dataset than dataReader.


这篇关于如何从SQL数据库访问/搜索数据并将其显示在带有文本框的表单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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