繁体   English   中英

如何在Visual Studio 2008中使用C#将远程数据库值检索到文本框中

[英]How to retrieve Remote Database values into text box using C# in Visual Studio 2008

美好的一天!。 我正在尝试检索远程数据库值并显示到Windows窗体文本框中。

我只能将字符串值检索到文本框中。 但是无法检索整数值。 如何获取整数值。

TabPage tpge = new TabPage();
tpge = tabControl1.SelectedTab;

SqlConnection cn = null;
string connectString = "";
SqlCommand cmd = null;
SqlDataReader rdr = default(SqlDataReader);

connectString = string.Format("Data Source = IP,1433;Initial Catalog=idb;Persist Security Info=True;User ID=USERNAME;Password=PASSWORD;");
cn = new SqlConnection(connectString);

try
{
    if (cn.State == ConnectionState.Closed)
    {
        cn.Open();
    }
}
catch (Exception ex)
{
    MessageBox.Show("Open Error");
    MessageBox.Show(ex.Message);
}
try
{
    MessageBox.Show("1");
    cmd = new SqlCommand("select * from tAerator1", cn);
    cmd.CommandType = CommandType.Text;
    rdr = cmd.ExecuteReader();
    while (rdr.Read())
    {
        txtAEremrk.Text = rdr.GetString(rdr.GetOrdinal("fldAEremrk"));
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

cn.Close();

您将使用ExecuteReader的“ GetInt”方法之一返回整数结果。

GetInt16

以16位带符号整数获取指定列的值。 (重写DbDataReader.GetInt16(Int32)。)

GetInt32等

以32位带符号整数获取指定列的值。 (重写DbDataReader.GetInt32(Int32)。)

GetInt64

以64位带符号整数获取指定列的值。 (重写DbDataReader.GetInt64(Int32)。)

例子

rdr.GetInt32(rdr.GetOrdinal("yourIntegerField"));

我只能将字符串值检索到文本框中。 但是无法检索整数值。 如何获取整数值。

仅返回整数值是不够的。 该文本框仅接受字符串值。 还需要ToString()方法将返回的整数值转换为字符串。

txtAEremrk.Text  = rdr.GetInt32(rdr.GetOrdinal("yourIntegerField")).ToString()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM