【问题标题】:SQL statement with a lot of join and parameters带有大量连接和参数的 SQL 语句
【发布时间】:2016-12-01 05:36:07
【问题描述】:

我有一个很长的选择语句,其中包含很多参数和内部连接。

我的第一个问题:有没有办法让它更有效率?

我的第二个问题是为什么它不在文本框中显示任何内容?

我的目标是显示来自搜索的房子。

用户首先选择组合框中的所有内容(所有参数),然后我的选择会遍历表格属性并在文本框中显示可用的房屋

        clsDataSource.mycon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\...");
        clsDataSource.mycon.Open();

        OleDbCommand mycmd = new OleDbCommand("SELECT AreaSize.*, Bathrooms.*, Cities.*, Prices.*,Properties.*, Rooms.*, Types.*, Users.* FROM Users INNER JOIN (Types INNER JOIN (Rooms INNER JOIN (Prices INNER JOIN (Cities INNER JOIN (Bathrooms INNER JOIN (AreaSize INNER JOIN Properties ON AreaSize.AreaSizeID = Properties.AreaSize) ON Bathrooms.BathroomID = Properties.Bathrooms) ON Cities.CityID = Properties.City) ON Prices.PriceID = Properties.Price) ON Rooms.RoomID = Properties.Rooms) ON Types.TypeID = Properties.PropertyType) ON Users.UserID = Properties.AgentID WHERE Properties.PropertyType=@propertyType AND Properties.City=@city AND Properties.Rooms=@rooms AND Properties.AreaSize=@areasize AND Properties.Price=@price AND Properties.Bathrooms=@bathrooms AND (Properties.BoolAgent = true)", clsDataSource.mycon);

        mycmd.Parameters.Add("@city", OleDbType.Integer, 3).Value = clsHouses.location;
        mycmd.Parameters.Add("@propertyType", OleDbType.Integer, 3).Value = clsHouses.type;
        mycmd.Parameters.Add("@rooms", OleDbType.Integer, 3).Value = clsHouses.bedrooms;
        mycmd.Parameters.Add("@areasize", OleDbType.Integer, 3).Value = clsHouses.surface;
        mycmd.Parameters.Add("@price ", OleDbType.Integer, 3).Value = clsHouses.price;
        mycmd.Parameters.Add("@bathrooms", OleDbType.Integer, 3).Value = clsHouses.bathrooms;

        myadaptS = new OleDbDataAdapter(mycmd);
        myadaptS.Fill(clsDataSource.myset, "ResultSearch");
        tbSearchResult = clsDataSource.myset.Tables["Properties"];


        txtType.Text = tbSearchResult.Rows[idx]["DescriptionType"].ToString();

【问题讨论】:

  • 我会在您的联接之上的数据库中创建一个视图。并从中选择而不是表格。

标签: c# sql ado.net


【解决方案1】:
        clsDataSource.mycon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\...");
        clsDataSource.mycon.Open();
        clsDataSource.myset = new DataSet();
        current = 0;

        OleDbCommand mycommand = new OleDbCommand("SELECT AreaSize.*, Bathrooms.*, Cities.*, Prices.*,Properties.*, Rooms.*, Types.*, Users.* FROM Users INNER JOIN (Types INNER JOIN (Rooms INNER JOIN (Prices INNER JOIN (Cities INNER JOIN (Bathrooms INNER JOIN (AreaSize INNER JOIN Properties ON AreaSize.AreaSizeID = Properties.AreaSize) ON Bathrooms.BathroomID = Properties.Bathrooms) ON Cities.CityID = Properties.City) ON Prices.PriceID = Properties.Price) ON Rooms.RoomID = Properties.Rooms) ON Types.TypeID = Properties.PropertyType) ON Users.UserID = Properties.AgentID WHERE Properties.PropertyType=@propertyType AND Properties.City=@city AND Properties.Rooms=@rooms AND Properties.AreaSize=@areasize AND Properties.Price=@price AND Properties.Bathrooms=@bathrooms AND (Properties.BoolAgent = true)", clsDataSource.mycon);
        mycommand.Parameters.Add("@propertyType", OleDbType.Integer).Value = clsHouses.type;
        mycommand.Parameters.Add("@city", OleDbType.Integer).Value = clsHouses.location;
        mycommand.Parameters.Add("@rooms", OleDbType.Integer).Value = clsHouses.bedrooms;
        mycommand.Parameters.Add("@areasize", OleDbType.Integer).Value = clsHouses.surface;
        mycommand.Parameters.Add("@price", OleDbType.Integer).Value = clsHouses.price;
        mycommand.Parameters.Add("@bathrooms", OleDbType.Integer).Value = clsHouses.bathrooms;
        mycommand.Parameters.Add("@address", OleDbType.VarChar).Value = txtAddress.Text;
        mycommand.Parameters.Add("@description", OleDbType.VarChar).Value = txtDescription.Text;

        mycommand.CommandType = CommandType.Text;


        myadaptL = new OleDbDataAdapter(mycommand);
        myadaptL.Fill(clsDataSource.myset, "Properties");
        tbListing = clsDataSource.myset.Tables["Properties"];
        //FillCombobox();
        TAB2TXT(current);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多