我如何使用aspx SQL查询 [英] How do I use aspx SQL query

查看:102
本文介绍了我如何使用aspx SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用文本框查询我的数据库并将结果输出到列表框中,以便用户可以选择哪个是正确的条目,因为有多个结果具有相似的信息。





我有一个小规模的列表框,但我需要在列表框中添加更多字段,但我只能获得2列显示我已经尝试添加更多,但我无法使代码工作。

使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Data;
使用System.Data.SqlClient;
使用System.Configuration;

命名空间Thomas_County_Public_Works.Sign_Database
{
公共部分类库存:System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{

}

protected void Button1_Click(object sender,EventArgs e)
{
ListBox1.Items.Clear() ;
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings [dbconnect]。ConnectionString);

SqlCommand command = new SqlCommand();

command.Connection = connection;
command.CommandText =Select * From SignDatabaseInventory;
command.CommandType = CommandType.Text;

尝试
{
connection.Open();

SqlDataReader reader = command.ExecuteReader();

while(reader.Read())
{
string title =(string)reader [MUTCD];
string description =(string)reader [Sign];

string item = string.Format({0} - {1},title,description);

this.ListBox1.Items.Add(item);

}

reader.Close();
}
catch
{
}
finally
{

if(connection.State == ConnectionState.Open)
connection.Close();
}
}
}
}

e



我有什么试过:



 {
string MUTCD =(string)reader [MUTCD];
string Sign =(string)reader [Sign];
string Sign_Type =(string)reader [Sign_Type];

string item = string.Format({0} - {1},MUTCD,Sign,Sign_Type);

this.ListBox1.Items.Add(item);

}

reader.Close();





我添加了Sign_Type但是我无法在列表框中显示第三项。感谢您的时间。

解决方案

:叹息:

尝试将第三个参数添加到格式字符串:

  string  item =  string  .Format(  {0}  -  {1}  -  {2},MUTCD,Sign,Sign_Type); / pre> 


I'm trying to use a text box to query my database and output the results into a list box so that the user can select which is the correct entry since there are multiple results with similar information.


I've got the list box to work on a small scale but I need to add more fields to the list box but I can only get 2 columns to show I've tried adding more but I can't get the code to work.

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

namespace Thomas_County_Public_Works.Sign_Database
{
    public partial class Inventory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ListBox1.Items.Clear();
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnect"].ConnectionString);

            SqlCommand command = new SqlCommand();

            command.Connection = connection;
            command.CommandText = "Select * From SignDatabaseInventory ";
            command.CommandType = CommandType.Text;

            try
            {
                connection.Open();

                SqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    string title = (string)reader["MUTCD"];
                    string description = (string)reader["Sign"];

                    string item = string.Format("{0} - {1}", title, description);

                    this.ListBox1.Items.Add(item);
                    
                }

                reader.Close();
            }
            catch
            {
            }
            finally
            {
                
                if (connection.State == ConnectionState.Open)
                    connection.Close();
            }
        }
    }
}

e

What I have tried:

{
                    string MUTCD = (string)reader["MUTCD"];
                    string Sign = (string)reader["Sign"];
                    string Sign_Type = (string)reader["Sign_Type"];

                    string item = string.Format("{0} - {1}", MUTCD, Sign, Sign_Type);

                    this.ListBox1.Items.Add(item);
                    
                }

                reader.Close();



I Added Sign_Type but I'm not able to show the third item in the list box. Thanks for your time.

解决方案

:sigh:
Try adding the third parameter to the format string:

string item = string.Format("{0} - {1} - {2}", MUTCD, Sign, Sign_Type);/pre>


这篇关于我如何使用aspx SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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