如何添加本地数据库项目到textBox使用listBox在C# [英] How to add local database items into textBox using listBox in C#

查看:206
本文介绍了如何添加本地数据库项目到textBox使用listBox在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人知道,我有一个问题:
我必须做什么,使用名字+姓氏,电子邮件和地址回到他们的文本框中添加选择的列表框值?

And i have a question if anyone knows: What would i have to do to add the selected listBox values using the "Firstname" + "Lastname", "Email" and "Address" back into their textboxes?

代码在此处允许我将我的textBox值添加到数据库中,但是id相反。

This code here lets me add my textBox values into database but id like to do the opposite.

private void button_add_Click(object sender, EventArgs e)
{
var insertSQL = "INSERT INTO Inimesed (Firstname, Lastname, Email, Address) VALUES (Firstname, Lastname, Email, Address)";

string connectionString = @"Data Source=myDatabase;Password=xxxxxx;";
using (var cn = new SqlCeConnection(connectionString))
using (var cmd = new SqlCeCommand(insertSQL, cn))
    {
         cn.Open();

        cmd.Parameters.Add("@Firstname", SqlDbType.NVarChar);
        cmd.Parameters.Add("@Lastname", SqlDbType.NVarChar);
        cmd.Parameters.Add("@Email", SqlDbType.NVarChar);
        cmd.Parameters.Add("@Address", SqlDbType.NVarChar);

        cmd.Parameters["Firstname"].Value = textBox1_Firstname.Text;
        cmd.Parameters["Lastname"].Value = textBox2_Lastname.Text;
        cmd.Parameters["Email"].Value = textBox3_Email.Text;
        cmd.Parameters["Address"].Value = textBox4_Address.Text;
        cmd.ExecuteNonQuery();

    }
}

如何在C#中添加textBox项到本地数据库,但没有如何添加本地数据库项到textBox。我得到了所有的值。
我应该在foreach命令中使用foreach命令,if命令或if命令吗?

There are tons of tutorials on "How to add textBox items into local database in C#", but none "How to add local database items into textBox". I got all the values defined. Should i use "foreach" commands, "if" commands or "if" commands inside "foreach" commands?

任何帮助将是伟大的! p>

Any help would be great!

推荐答案

如何决定要检索哪一行?

How are you going to decide which row you want to retrieve?

类似于以下内容应该基于电子邮件地址从数据库检索单个行,然后使用值填充文本框:

Something like the following should retrieve a single row from the database based on email address, then use the values to populate the textboxes:

private void button_retrieve_Click(object sender, EventArgs e)
{
var selectSQL = "select Firstname, Lastname, Email, Address Inimesed where email = @email";

string connectionString = @"Data Source=myDatabase;Password=xxxxxx;";
using (var cn = new SqlCeConnection(connectionString))
using (var cmd = new SqlCeCommand(selectSQL, cn))
{
     cn.Open();

    cmd.Parameters.Add("@Email", SqlDbType.NVarChar);
    cmd.Parameters["Email"].Value = "emailaddresstofind";

    var rdr = cmd.ExecuteReader();
    try
    {
        if (rdr.Read())
        {
            textBox1_Firstname.Text = rdr.GetString(0);
            textBox2_Lastname.Text = rdr.GetString(1);
            textBox3_Email.Text = rdr.GetString(2);
            textBox4_Address.Text = rdr.GetString(3);
        }
        else
        {
            MessageBox.Show("Could not find record");
        }
    }    
    finally
    {
        rdr.Close();
        cn.Close();
    }
}

}

这篇关于如何添加本地数据库项目到textBox使用listBox在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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