试图在C#中查询数据库,然后将其存储到变量

这是我的主要目标:我希望用户能够在文本框中输入计算机名,并且我的数据库中有一个表,其中包含计算机名和在它的ipaddresses。所以我想能够做这样的事情,当我查询数据库... “从computername其中computername ='textbox1.text'”选择ipaddress。这样,当用户输入计算机名称时,它会查看数据库并使用ip地址映射到计算机。试图在C#中查询数据库,然后将其存储到变量

到目前为止我只是试图从数据库返回结果到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();

}

}

}

回答:

这里有一个例子,参考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();

}

}

}

回答:

此代码替换什么在你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的版本。

以上是 试图在C#中查询数据库,然后将其存储到变量 的全部内容, 来源链接: utcz.com/qa/258174.html

回到顶部