如何在C#中将不同类型的变量检索到文本框中 [英] How Can I Retrieve Differant Type Of Variables Into Textboxes In C#

查看:92
本文介绍了如何在C#中将不同类型的变量检索到文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发和应用了。

他们的功能是从bql server数据库中填充所有文本框。

所以sql数据库包含字符串值,int值,日期值。



应用程序中我有文本框,日期时间选择器,组合框。



  private   void  Search_Click( object  sender,EventArgs e)
{
string myselection = ViewClass.SearchWrd;
string connectionString = 数据源= PD -JANAKAN; Initial Catalog = EnqInfo; Integrated Security = True;
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand( SELECT * FROM登录WHERE Id == @ Id,连接);
command.Parameters.AddWithValue( @ Id,myselection);
SqlDataReader reader = command.ExecuteReader();


while (reader.Read())
{
string Cust_Name = reader.GetString( 3 ); // 名称字符串
if (reader [ Cust_Cntact]!= DBNull.Value)
{
int cust = Convert.ToInt32(reader [ Cust_Cntact ]);
}
dateTimePickerJoinDate.Value = dt.Rows [ 0 ] [ < span class =code-string> JoinDate
]。ToString();}







有人可以帮助我使用sql

中的sql日期值加载datetimepicker并使用数据库整数值加载文本框吗?

并使用数据库整数和字符串值加载组合框?



i猜我的代码错了,除了获取文本框的字符串值和文本框的数据库int值。



谢谢...



请帮我解决这个问题。大帮助............

解决方案

尝试:

 dateTimePickerJoinDate .Value =(DateTime)reader [  JoinDate]; 


几乎没有代码改进

1.请参阅文档并研究它们,例如,如果您阅读 SqlCommand.ExecuteReader方法 [ ^ ] MSDN文档页面:

示例代码:

< pre lang =c#> // using语句将在离开块时自动处理/关闭连接
使用(SqlConnection connection = new SqlConnection(
connectionString))
{
// 您的代码中没有打开连接
connection.Open();

SqlCommand command = new SqlCommand(queryString,connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine( String .Format( {0},读者[ 0 ]));
}
}



通过研究上述代码,您将学习使用声明等最佳实践,并确保您已打开在ExecuteReader等之前的连接..



2.不要在sql中使用双等号的条件,它应该如下所示

  SELECT  *  FROM 登录 WHERE  Id = @Id 





3.我们真的需要while循环吗?对于给定的ID,如果只有一个记录可用,则不会从sql语句中获得多个结果,并且如果在将值设置为while循环中的文本框时有多个记录,则将重新设置从数据库返回的每个记录,最后,您将在UI上拥有最后的记录数据。因此,如果你需要一个记录数据,你可以将while循环更改为if条件

  if ( reader.Read())
{
CustNameTxt.Text = reader.GetString( 3 );
// 其他字段值......
}





4.当您将值设置为控件的属性或程序中的某个其他字段时,您最好检查属性的类型。字段正在加工您要设置的值。例如 TextBox文本属性需要字符串类型 [ ^ ]你需要设置字符串类型的值。例如:

 CustNameTxt.Text = reader.GetString( 3 );上面的
// 将起作用,因为GetString将从数据库读取器返回字符串值
int age = 25 ;
TextBoxAge.Text = age.ToString();
// age是int值但Text属性需要字符串类型,ToString方法将其转换为字符串值



然后你如何设置日期时间的纠察队价值? 日期时间选择器需要什么类型?值属性 [ ^ ]?你如何阅读 sqlreader中的datetime列值 [ ^ ]?

如果您理解我的解释,您将找到上述问题的答案并解决上述代码中的问题。

祝你好运!


i have developed and application.
their is an function to
fill all the textboxes from sql server database.
so sql database contains string values, int values, date values.

in application i have textboxes, datetimepickers, comboboxes.

private void Search_Click(object sender, EventArgs e)
        {
            string myselection = ViewClass.SearchWrd;
            string connectionString = "Data Source=PD-JANAKAN;Initial Catalog=EnqInfo;Integrated Security=True";
            SqlConnection connection = new SqlConnection(connectionString);
            SqlCommand command = new SqlCommand("SELECT * FROM Login WHERE Id==@Id", connection);
            command.Parameters.AddWithValue("@Id", myselection);
            SqlDataReader reader = command.ExecuteReader();


            while (reader.Read())
            {
                string Cust_Name = reader.GetString(3);  // Name string
                if (reader["Cust_Cntact"] != DBNull.Value)
                {
                    int cust = Convert.ToInt32(reader["Cust_Cntact"]);
                }
                dateTimePickerJoinDate.Value = dt.Rows[0]["JoinDate"].ToString();}




can someone help me to load datetimepicker using sql date value from sql
and load textbox using database integer values?
and load comboboxes using database integer and string values?

i guess my code is wrong except get string values to textboxes and database int values to textboxes.

Thanks...

please help me to solve this. big help............

解决方案

Try:

dateTimePickerJoinDate.Value = (DateTime) reader["JoinDate"];


Few code improvements
1. Refer the documentation and study them, for example if you read SqlCommand.ExecuteReader Method[^] MSDN documentation page:
sample code:

//using statement will dispose/close the connection automatically when it leave the block
using (SqlConnection connection = new SqlConnection(
           connectionString))
{
    // you haven't open connection in your code
    connection.Open();

    SqlCommand command = new SqlCommand(queryString, connection);
    SqlDataReader reader = command.ExecuteReader();
    while (reader.Read())
    {
        Console.WriteLine(String.Format("{0}", reader[0]));
    }
}


by studying above code you will learn best practices like "using" statement and make sure that you have open the connection before the ExecuteReader etc..

2. don't use double equal sign in sql where condition, it should be like below

SELECT * FROM Login WHERE Id=@Id



3. Do we really need while loop here? for given ID if only one record available you don't get multiple results from the sql statement and also if you have multiple records when you set value to a text box inside while loop it will re set every record which return from the database, at the end you will have last record data on the UI. So you can change the while loop to if condition if you need one record data as below

if (reader.Read())
{
  CustNameTxt.Text = reader.GetString(3);
  // other field values......
}



4. when you set value to a property of a control or some other field in your program you better check the type of the property\field is maching with the value you going to set. for example TextBox Text Property need string type[^] and you need to set value in string type. for example :

 CustNameTxt.Text = reader.GetString(3);
//above will work because GetString will return string value form database reader
 int age =25;
 TextBoxAge.Text = age.ToString();
// age is int value but Text property need string type, ToString method convert it to a string value


then how you set datetime picket value? what is the type need for datetime picker value property[^]? how you read datetime column value from sqlreader[^]?
if you understand my explanation you will find answers to above questions and fix the issues in above code.
Good luck!


这篇关于如何在C#中将不同类型的变量检索到文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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