0

这是我的主要目标:我希望用户能够在文本框中输入计算机名,并且我的数据库中有一个表,其中包含计算机名和 IP 地址。因此,当我查询数据库时,我希望能够做这样的事情......“从计算机名中选择 ipaddress,其中计算机名 = 'textbox1.text'”。这样,当用户输入计算机名称时,它将在 db 中查找并使用 ipaddress 映射到 pc。

到目前为止,我只是想将结果从数据库返回到 textbox1。任何帮助表示赞赏。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace testwf
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitializeComponent();

                        SqlConnection cs = new SqlConnection(@"Server=10-nuerp-006acdst;Database=Rert;User Id=reports;Password=Password");
            SqlDataAdapter da = new SqlDataAdapter();
            cs.Open();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection cs = new SqlConnection(@"Server=10-nuerp-006acdst;Database=Rert;User Id=reports;Password=Password
            SqlDataAdapter da = new SqlDataAdapter();
            cs.Open();
            da.SelectCommand = new SqlCommand();
            SqlCommand command = new SqlCommand("select top 1 * from station", cs);
            SqlDataReader dr = command.ExecuteReader();
            //cs.Open();
            dr.Read();

            while (dr.Read())
            {
                cs.Open();
                command.ExecuteReader();
                textbox1.Text = dr.GetSqlValue(1).ToString();
                MessageBox.Show(dr.GetSqlValue(0).ToString());
                            }

            MessageBox.Show(dr.GetSqlValue(0).ToString());
            cs.Close();

        }
    }
}
4

2 回答 2

1

这是一个例子,参考http://www.akadia.com/services/dotnet_data_reader.html

 private void btnFind_Click(object sender, System.EventArgs e)
    {
        SqlDataReader rdr = null;
        SqlConnection con = null;
        SqlCommand cmd = null;

        try
        {
            // Open connection to the database
            string ConnectionString = "server=xeon;uid=sa;"+
                "pwd=manager; database=northwind";
            con = new SqlConnection(ConnectionString);
            con.Open();

            // Set up a command with the given query and associate
            // this with the current connection.
            string CommandText = "SELECT FirstName, LastName" +
                                 "  FROM Employees" +
                                 " WHERE (LastName LIKE @Find)";
            cmd = new SqlCommand(CommandText);
            cmd.Connection = con;

            // Add LastName to the above defined paramter @Find
            cmd.Parameters.Add(
                new SqlParameter(
                "@Find", // The name of the parameter to map
                System.Data.SqlDbType.NVarChar, // SqlDbType values
                20, // The width of the parameter
                "LastName"));  // The name of the source column

            // Fill the parameter with the value retrieved
            // from the text field
            cmd.Parameters["@Find"].Value = txtFind.Text;

            // Execute the query
            rdr = cmd.ExecuteReader();

            // Fill the list box with the values retrieved
            lbFound.Items.Clear();
            while(rdr.Read())
            {
                lbFound.Items.Add(rdr["FirstName"].ToString() +
                " " + rdr["LastName"].ToString());
            }
        }
        catch(Exception ex)
        {
            // Print error message
            MessageBox.Show(ex.Message);
        }
        finally
        {
            // Close data reader object and database connection
            if (rdr != null)
                rdr.Close();

            if (con.State == ConnectionState.Open)
                con.Close();
        }
    }
}
于 2012-11-19T15:30:23.627 回答
0

此代码替换您的button1_click事件中的内容:

using (SqlConnection cs = new SqlConnection(@"Server=10-nuerp-006acdst;Database=Rert;User Id=reports;Password=Password"))
{
    cs.Open();

    using (SqlCommand cmd = new SqlCommand("select top 1 * from station", cs))
    using (SqlDataReader dr = command.ExecuteReader())
    {
        while (dr.Read())
        {
            textbox1.Text = dr.GetSqlValue(1).ToString();
            MessageBox.Show(dr.GetSqlValue(0).ToString());
        }
    }
}

至于“uelreader”的回答:添加这样的 SQL 参数要容易得多:

cmd.Parameters.AddWithValue("@parameterName", someVariableOrConstant);

类型是自动确定的,转换是由框架完成的。仅当您需要明确设置类型和长度时才使用 urlreader 的版本。

于 2012-11-19T16:03:25.823 回答