SQL命令中的OR语句不起作用

时间:2017-01-04 06:59:30

标签: sql sql-server

有谁知道修复错误?我想创建OR语句但它在我的代码中不起作用。代码如下所示。我删除了OR以查看代码功能,如果删除or语句,它可以起作用。所以我非常想知道代码的真正问题是什么。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data; 

public partial class updateform : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=test;Integrated Security=True");
        SqlDataAdapter sda = new SqlDataAdapter("Select * FROM [EMP] WHERE Serial_Num='" + TextBox2.Text + "' or Equipment_ID ='" + TextBox1.Text + "'", con);

        DataTable dt = new DataTable();
        sda.Fill(dt);
        TextBox3.Text = dt.Rows[0][2].ToString();
        TextBox4.Text = dt.Rows[0][6].ToString();
        DropDownList1.Text = dt.Rows[0][7].ToString();
        TextBox5.Text = dt.Rows[0][18].ToString();
        TextBox6.Text = dt.Rows[0][11].ToString();
        TextBox11.Text = dt.Rows[0][8].ToString();
        TextBox7.Text = dt.Rows[0][20].ToString();
        TextBox12.Text = dt.Rows[0][17].ToString();
        DropDownList2.Text = dt.Rows[0][23].ToString();
        TextBox9.Text = dt.Rows[0][14].ToString();
        TextBox10.Text = dt.Rows[0][13].ToString();
        TextBox13.Text = dt.Rows[0][12].ToString();
        TextBox2.Text = dt.Rows[0][5].ToString();
        TextBox1.Text = dt.Rows[0][4].ToString();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source =.\\sqlexpress; Initial Catalog = test; Integrated Security = True");
        con.Open();

        SqlCommand cmd = new SqlCommand(@"UPDATE EMP SET Model='" + TextBox3.Text + "' ,Description= '" + TextBox4.Text + "' ,Location='" + DropDownList1.Text + "',Manufacturer_or_Vendor= '" + TextBox5.Text + "',NCR_or_OOT_History='" + TextBox6.Text + "',Due_date= '" + TextBox11.Text + "' ,Year_of_Manufacturing='" + TextBox12.Text + "',Asset_No= '" + TextBox7.Text + "' ,Status='" + DropDownList2.Text + "',Responsible_Person= '" + TextBox9.Text + "',Available_in_Sapphire= '" + TextBox10.Text + "',Last_OOT_issuance_Date= '" + TextBox13.Text + "' WHERE (Serial_Num = '" + TextBox2.Text + "' or Equipment_ID = '" + TextBox1.Text + "' )", con);
        cmd.ExecuteNonQuery();
        con.Close();
    }

    protected void Page_load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Calendar1.Visible = false;
            Calendar2.Visible = false;
            Calendar3.Visible = false;
        }
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if (Calendar1.Visible)
        {
            Calendar1.Visible = false;
        }
        else
        {
            Calendar1.Visible = true;
        }
    }

    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
   {
        if (Calendar2.Visible)
        {
            Calendar2.Visible = false;
        }
        else
        {
            Calendar2.Visible = true;
        }
    }

    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        if (Calendar3.Visible)
        {
            Calendar3.Visible = false;
        }
        else
        {
            Calendar3.Visible = true;
        }
    }

protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
    TextBox11.Text = Calendar1.SelectedDate.ToString("dd/MM/yyyy");
    Calendar1.Visible = false;
}

protected void Calendar2_SelectionChanged(object sender, EventArgs e)
{
    TextBox12.Text = Calendar2.SelectedDate.ToString("dd/MM/yyyy");
    Calendar2.Visible = false;
}

protected void Calendar3_SelectionChanged(object sender, EventArgs e)
{
    TextBox13.Text = Calendar1.SelectedDate.ToString("dd/MM/yyyy");
    Calendar3.Visible = false;
}

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
    if (e.Day.IsOtherMonth)
    {
        e.Day.IsSelectable = false;

    }
}

protected void Calendar2_DayRender(object sender, DayRenderEventArgs e)
{
    if (e.Day.IsOtherMonth)
    {
        e.Day.IsSelectable = false;
    }
}

protected void Calendar3_DayRender(object sender, DayRenderEventArgs e)
{
    if (e.Day.IsOtherMonth)
    {
        e.Day.IsSelectable = false;
    }
}

protected void Button3_Click(object sender, EventArgs e)
{
    Response.Redirect("Default.aspx");
}
}

2 个答案:

答案 0 :(得分:0)

是否可以声明一个您为其分配sql语句的字符串,如下所示:

string sql ="Select * FROM [EMP] WHERE Serial_Num='" + TextBox2.Text + "' or Equipment_ID ='" + TextBox1.Text + "'";

在此行代码之后设置断点。然后运行程序。

将sql变量的值粘贴到sql server中,看看它的作用。如果它返回预期结果,则代码中的错误必须进一步缩小。

答案 1 :(得分:0)

从您的描述中不清楚它是在编译时或执行时失败,还是错误是什么。但是对于可能出现问题的猜测,请尝试以下方法:

  1. 而不是选择*尝试指定实际列(例如选择col1,col2,..

  2. 而不是:

    sda.Fill(DT);

  3. 声明数据集并填充数据集,看它是否有效。

    DataSet ds = new DataSet (); 
    sda.Fill (ds);
    

    如果此方法有效,请检查并查看数据集中创建的表数以及每个表中的记录数。