我有一个Windows窗体,我要求用户输入在TextBox1中pcname,然后试图用SqlDataReader
的从数据库中读取来获得PC ip地址,然后映射驱动器的PC到本地PC。
但由于某些原因,当我使用它不工作的SQL参数中的文本。 但是,当我更换textbox1.text
实际pcname它工作正常。 希望有人可以帮我看看为什么参数不能正常工作。
这里是我的代码:
public void button1_Click(object sender, EventArgs e)
{
string results = "";
using (SqlConnection cs = new SqlConnection(@"***removed connection string***"))
{
cs.Open();
string query = "select stationipaddress from station where stationname = @StationName";
using (SqlCommand cmd = new SqlCommand(query, cs))
{
// Add the parameter and set its value --
cmd.Parameters.AddWithValue("@StationName", textBox1.Text);
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
label3.Text = dr.GetSqlValue(0).ToString();
results = dr.GetValue(0).ToString();
MessageBox.Show(dr.GetValue(0).ToString());
MessageBox.Show(results);
}
string myvar = string.Format(@"use S: \\" + label3.Text + "\\c$\logs 0A36303 /user:admin", label3.Text);
Process p = new Process();
p.StartInfo.FileName = "net.exe";
p.StartInfo.Arguments = (myvar);
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.CreateNoWindow = true;
p.Start();