Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL命令中的OR语句不起作用_Sql_Sql Server - Fatal编程技术网

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

SQL命令中的OR语句不起作用,sql,sql-server,Sql,Sql Server,有人知道如何修复错误吗?我想创建或语句,但它在我的代码中不起作用。代码如下所示。我已经删除了或以查看代码是否起作用,如果我删除了OR语句,它可以起作用。所以我非常想知道我的代码的真正问题是什么 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.D

有人知道如何修复错误吗?我想创建
语句,但它在我的代码中不起作用。代码如下所示。我已经删除了
以查看代码是否起作用,如果我删除了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");
}
}

从您的描述中不清楚它是否在编译时或执行时失败,或者错误是什么。但是,如果想猜测出问题所在,请尝试以下方法:

  • 请尝试指定实际列(如Select col1、col2、…),而不是Select*命令

  • 而不是:

    sda.填充(dt)

  • 声明一个数据集并填充该数据集,然后查看它是否有效

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

    如果可行,请检查数据集中创建了多少个表,以及每个表中有哪些记录。

    您应该使用准备好的语句,这可能会避免您现在遇到的一些麻烦。它如何“不起作用”?请添加错误消息或预期和实际输出。@piet.t数据不显示在文本框中。我的代码的用途是当用户单击“搜索”按钮时。数据将显示在文本框中。但是,它仅在删除or语句时显示。它不显示在文本框中。我的代码的用途是当用户单击“o”时n搜索按钮。数据将显示在文本框中。但是,仅当我删除or语句时才会显示。