C# xt; //检查addresses变量中是否有空格 if(地址.包含(“”)) { 地址=地址。替换(“,”); } //通过查找“,”来搜索多个搜索项 if(地址.包含(“,”)) { 字符串[]IParray=addresses.Split(',');
xt; //检查addresses变量中是否有空格 if(地址.包含(“”)) { 地址=地址。替换(“,”); } //通过查找“,”来搜索多个搜索项 if(地址.包含(“,”)) { 字符串[]IParray=addresses.Split(','); string[]Parameters=IParray.Select((IP,index)=>“@IP”+index.ToString()).ToArray(); string commandformat=“从({0})中IP_地址所在的位置选择*”; 字符串参数TXT=string.Join(“,”参数); string commandtxt=string.Format(commandformat,parametertxt); //创建SQL连接“连接”打开连接创建要执行的SQL命令&绑定并刷新gridview 使用(SqlConnection=newsqlconnection(“数据源=localhost;初始目录=Inventory;集成安全=True”)) { SqlDataReader=null; connection.Open(); SqlCommand=newsqlcommand(commandtxt,connection);C# xt; //检查addresses变量中是否有空格 if(地址.包含(“”)) { 地址=地址。替换(“,”); } //通过查找“,”来搜索多个搜索项 if(地址.包含(“,”)) { 字符串[]IParray=addresses.Split(',');,c#,sql,asp.net,sql-server,C#,Sql,Asp.net,Sql Server,xt; //检查addresses变量中是否有空格 if(地址.包含(“”)) { 地址=地址。替换(“,”); } //通过查找“,”来搜索多个搜索项 if(地址.包含(“,”)) { 字符串[]IParray=addresses.Split(','); string[]Parameters=IParray.Select((IP,index)=>“@IP”+index.ToString()).ToArray(); string commandformat=“从({0})中IP_地址所在的位置选
对于(int i=0;iYou通过在文本框中用逗号分隔多个IP来表示处理多个IP的UI,但您的代码似乎暗示了一个下拉列表。我的错误是,我从程序的另一个区域复制了此代码,但尚未完全调整,现在将更改:)您通过在文本框中用逗号分隔多个IP来表示处理多个IP的UI,但您的代码似乎暗示了一个下拉列表。我的错误是,我从程序的另一个区域复制了这段代码,但尚未完全修改,现在将更改:)谢谢@walther,这对我来说完全有意义。我想我不确定如何编译all给定值的数量,一个查询中的值是不确定的。我看到的唯一问题是
IN
子句将是一个字符串,但其中的值不会。这意味着它将是IN('192.168.0.1192.168.0.2')
而不是IN('192.168.0.1','192.168.0.2'))
谢谢@walther,这对我来说完全有意义。鉴于值的数量不确定,我想我不确定如何将所有值编译成一个查询。我看到的唯一问题是,您的IN
子句将是一个字符串,但其中的值不会。这意味着它将是IN('192.168.0.1192.168.0.2'))
而不是('192.168.0.1'、'192.168.0.2'中的)
谢谢你的回答@Robby Cornelissen我会试一试:)谢谢你的回答@Robby Cornelissen我会试一试:)我没有跟踪你@Patel。如上所述,用户会在文本框中输入一个或多个ip地址。这些ip地址不会在每次搜索时都相同,因此需要获取来自UI的参数。这就是我所做的。我编辑了我的问题。我还添加了一条从UI获取IP的注释。我只写了一个静态IP。例如,我没有跟踪你@Patel。如上所述,用户将在文本框中输入一个或多个IP地址。每次进行搜索时,这些IP地址将不相同,因此需要从用户界面获取参数。这就是我所做的。我编辑了我的问题。我还添加了一条从用户界面获取IP的注释。我只编写了静态IP作为示例
protected void siteSearchButton_Click(object sender, EventArgs e)
{
//checks IP search textbox is empty
if (ipQueryTextBox.Text != null)
{
searchErrorLabel.Visible = false;
string addresses = ipQueryTextBox.Text;
//checks for any blank spaces in the addresses variable
if (addresses.Contains(" "))
{
addresses = addresses.Replace(" ", "");
}
//sceens for multiple search items by looking for a ','
if (addresses.Contains(","))
{
//declaring int variables to be used in each of the respective loops
int j = 0;
string[] IParray = addresses.Split(',');
//if i is equal to the length of the "addresses" variable, execute the for loop enclosed
foreach (string s in IParray)
{
Session["IP" + j] = IParray[j];
j = j + 1;
}
}
}
}
//****THE SQL COMMAND BELOW NEEDS ADAPTING TO ALLOW MULTIPLE QUERIES FOR EACH OF THE VALUES STORED IN IParray ---> each session variable
if()
{
//opens a new sqlconnection to read and populate edit textboxes from the Inventory database
using (SqlConnection connection = new SqlConnection("Data Source=localhost;Initial Catalog=Inventory;Integrated Security=True"))
{
//declares SQLCommand type named 'command' and assigns it a string value of SQL code
SqlCommand command =
new SqlCommand("select * from LOCATION WHERE IP_ADDRESS=@IP_ADDRESS", connection);
//outlines parameters
command.Parameters.Add("@IP_ADDRESS", System.Data.SqlDbType.VarChar);
command.Parameters["@IP_ADDRESS"].Value = Session["IP"+j];;
connection.Open();
//opens database connection
SqlDataReader read = command.ExecuteReader();
//while loop will convert each record to string value and print entry into textbox. Will continue untill it runs out of lines
while (read.Read())
{
}
read.Close();
}
}
if (ipQueryTextBox.Text != null)
{
searchErrorLabel.Visible = false;
string addresses = ipQueryTextBox.Text;
addresses = addresses.Replace(" ", "");
addresses = addresses.Replace(",", "','");
Session["addresses"] = addresses;
}
SqlCommand command = new SqlCommand("select * from LOCATION WHERE IP_ADDRESS IN (@IP_ADDRESSES)", connection);
command.Parameters.AddWithValue("IP_ADDRESSES", Session["addresses"]);
string[] ips = new string[] { "192.168.0.1", "192.168.0.2", "192.168.0.3" };
string[] parameters = ips.Select(
(ip, index) => "@ip" + index.ToString()
).ToArray();
string commandFormat = "SELECT * FROM LOCATION WHERE IP_ADDRESS IN ({0})";
string parameterText = string.Join(",", parameters);
string commandText = string.Format(commandFormat, parameterText);
using (SqlCommand command = new SqlCommand(commandText)) {
for(int i = 0; i < parameters.Length; i++) {
command.Parameters.AddWithValue(parameters[i], ips[i]);
}
}
string IPAddress = ipQueryTextBox.Text; //e.g. "192.168.0.1,192.168.0.2,192.168.0.3"
string commandFormat = "SELECT * FROM LOCATION WHERE IP_ADDRESS IN ('" + string.Join("','", IPAddress.split(",")) + "')";
protected void siteSearchButton_Click(object sender, EventArgs e)
{
//checks IP search textbox is empty
if (ipQueryTextBox.Text != null)
{
searchErrorLabel.Visible = false;
string addresses = ipQueryTextBox.Text;
//checks for any blank spaces in the addresses variable
if (addresses.Contains(" "))
{
addresses = addresses.Replace(" ", "");
}
//sceens for multiple search items by looking for a ','
if (addresses.Contains(","))
{
string[] IParray = addresses.Split(',');
string[] Parameters= IParray.Select((IP, index)=>"@ip"+ index.ToString()).ToArray();
string commandformat ="SELECT * FROM LOCATION WHERE IP_ADDRESS IN ({0})";
string parametertxt= string.Join(",",Parameters);
string commandtxt= string.Format(commandformat,parametertxt);
//creates an SQL connection "connection" opens the connection creates the sql command to be executed & binds and refreshes the gridview
using (SqlConnection connection = new SqlConnection("Data Source=localhost;Initial Catalog=Inventory;Integrated Security=True"))
{
SqlDataReader reader = null;
connection.Open();
SqlCommand command = new SqlCommand(commandtxt, connection);
for(int i =0; i<Parameters.Length; i++)
{
command.Parameters.AddWithValue(Parameters[i],IParray[i]);
}
reader = command.ExecuteReader();
browseSiteGridView.DataSource = reader;
browseSiteGridView.DataBind();
reader.Close();
connection.Close();
}
}
else
{
//creates an SQL connection "connection" opens the connection creates the sql command to be executed & binds and refreshes the gridview
string commandtxt="SELECT * FROM LOCATION WHERE IP_ADDRESS ='"+addresses+"'";
using (SqlConnection connection = new SqlConnection("Data Source=localhost;Initial Catalog=Inventory;Integrated Security=True"))
{
SqlDataReader reader = null;
connection.Open();
SqlCommand command = new SqlCommand(commandtxt, connection);
reader = command.ExecuteReader();
browseSiteGridView.DataSource = reader;
browseSiteGridView.DataBind();
reader.Close();
connection.Close();
}
}
}