从c#.net中的数据库中检索文本框中的数据 [英] retrive data in textbox from database in c#.net

查看:73
本文介绍了从c#.net中的数据库中检索文本框中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c#.net中,我创建了两种形式.
我的第一种形式是放置一个文本框和一个下拉列表框,然后给每个框指定值并存储在sql Sever 2000中.
然后第二种形式我放置一个下拉列表框和文本框.在该下拉列表框中,我从SQL Server检索数据,这是第一个表单下拉列表box1.

现在我想从第一个表单文本框中检索数据.

怎么可能.
请帮助我.....

In c#.net i created two forms.
First form i placed one text box and one drop down list box then given values to each box and stored in sql sever 2000.
Then second form i place one drop down list box and text box. In that drop down list box i retrieve data from sql server which is first form drop down list box1.

Now i want to retrieve data from first form text box.

How is it possible.
Please help me.....

推荐答案

using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))

using (var command = connection.CreateCommand())
{
    command.CommandText = "SELECT ColumnName FROM [TableName]";
    connection.Open();
    using (var reader = command.ExecuteReader())
    {
        while (reader.Read())
            TextBox1.Text = reader["ColumnName"].ToString();
    }
}


一些评论:
-不要使用*,仅指定您需要的字段
-使用-减少代码,保证处置
-我假设这是一个测试程序,否则在循环中将Text重置为a不会有意义.


Some comments:
-don''t use *, specify only those fields that you need
-use using - less code, guaranteed disposal
-I assume this is a test program, otherwise it does not make sense to reset Text to a in the loop


这很简单..

请按照以下步骤操作:
1)转到您的form2.designer.cs
2)在form2中为您的文本框找到它(记住它是form2)
It is Quite simple..

Follow this steps:
1)Go to your form2.designer.cs
2)Find this for your textbox in form2(Remember it''s form2)
private System.Windows.Forms.TextBox txtBox;


3)进行如下更改


3) Change it as follows

public System.Windows.Forms.TextBox txtBox;


4)进入表单1,在该事件中您要更改表单的任何事件并创建第二个表单的对象,然后像这样调用第二个表单中的文本框,并将值从form1发送到form2


4) Come to form 1 where on any event through which you are changing the form and create the object of second form and call the textbox which is in your second form like this and send the value from your form1 to form2

form2 f=new form2();
f.txtBox=TextBox1.Text;
this.Hide();
f.Show();


只需尝试Amit Kumar 1的解决方案1.一定会起作用的
当您创建一个公共变量时,您也可以全局使用它...!
just try solution 1 of Amit Kumar 1..! it will definatly work
when u create a public variable then u can use it globali also...!


这篇关于从c#.net中的数据库中检索文本框中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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