SqlDataReader的参数不工作 [英] SqlDataReader parameter not working

查看:206
本文介绍了SqlDataReader的参数不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows窗体,我要求用户输入TextBox1中pcname,然后试图用 SqlDataReader的来读取从数据库中获取的PC IPADDRESS然后在PC驱动器映射到本地PC。



但由于某些原因,当我使用它不工作的SQL参数中的文本框。但是,当我更换 textbox1.text 与实际pcname它工作正常。希望有人可以帮我找出为什么参数不正常工作。



下面是我的代码:

 公共无效的button1_Click(对象发件人,EventArgs五)
{
字符串结果=;使用(SqlConnection的CS =新的SqlConnection(@删除***连接字符串***))
{
cs.Open()

;

查询字符串=选择站,工作站名称= @StationName stationipaddress;使用

(CMD的SqlCommand =新的SqlCommand(查询,CS))
{
//添加参数,并将其值设置 -
cmd.Parameters.AddWithValue (@StationName,textBox1.Text);使用

(SqlDataReader的博士= cmd.ExecuteReader())
{
,而(dr.Read())
{
label3.Text =博士.GetSqlValue(0)的ToString();
结果= dr.GetValue(0)的ToString();
MessageBox.Show(dr.GetValue(0)的ToString());
MessageBox.Show(结果);
}

串MYVAR =的String.Format(@使用s:\\+ label3.Text +\\c $ \logs 0A36303 /用户:admin label3.Text);

进程p =新工艺();
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();


解决方案

也许如果你分开,你在不同的代码行一设置您的参数,和一个加它,你可以更清楚地看到问题的所在。
事情是这样的:

 的SqlParameter参数1 =新的SqlParameter(@工作站名称,SqlDbType.NVarChar,textBox1.Text 。长度); 
param1.Value = textBox1.Text;
cmd.Parameters.Add(参数1);



至少更容易看到正在发生的事情在调试器。


I have a windows form that I am asking a user to enter a pcname in textbox1 and then trying to use SqlDataReader to the read from the database to get the pc ipaddress and then map the pc drive to my local pc.

But for some reason when I use the textbox within the SQL parameter it's not working. But when I replace the textbox1.text with the actual pcname it works fine. Hopefully someone can help me find out why the parameter isn't working correctly.

Here is my code:

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();

解决方案

Maybe if you separate you code in different lines, one to set up your parameter, and one to add it, you can better see where the problem is. Something like this:

SqlParameter param1 = new SqlParameter("@StationName", SqlDbType.NVarChar, textBox1.Text.length);
param1.Value = textBox1.Text;
cmd.Parameters.Add(param1);

at least is easier to see what is going on on the debugger.

这篇关于SqlDataReader的参数不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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