添加列表在 C# 中搜索子字符串
add list the Search a substring in C#
我正在创建应用程序,但我有一个问题。
客户端在文本框中输入用户名,例如3个字母并在数据库(access)中搜索并添加数据库。
示例:用户:Rui。
并在数据库中搜索所有名称用户“Rui”。
//libraries
using Microsoft.VisualStudio.OLE.Interop;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
private void textBox1_TextChanged(object sender, EventArgs e)
{
OleDbConnection conexao = new OleDbConnection(string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= {0}\Teste.accdb", Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)));
List<string> Users = new List<string>();
OleDbCommand STK = new OleDbCommand($"SELECT NºCliente, NomeUser, CodigoPostal, NIF", conexao);
STK.CommandText = $" SELECT* FROM MyTable WHERE Str(Lista_Pokemon) like '*{textBox1.Text}*'";
User.Clear();
//this code is invention, probably is wrong
for(int d=0; d<Stk.Count()-1; d++)
User.Add(...);
}
如果你能帮助我谢谢。这个项目是c#,net framework,数据库是Access 2010。目前我没有创建class,但是如果你需要告诉我,我需要创建。
您需要创建一个 DbReader 并移动到下一行直到结束:
OleDbCommand STK = new OleDbCommand($"SELECT NºCliente, NomeUser, CodigoPostal, NIF", conexao);
STK.CommandText = $" SELECT * FROM MyTable WHERE Str(Lista_Pokemon) like '%{textBox1.Text}%'";
Users.Clear();
var reader = STK.ExecuteReader();
while (reader.Read())
Users.Add(reader["Lista_Pokemon"].ToString());
就安全性而言,将用户输入线程化到查询文本中被认为是一种危险的做法,而且在逻辑上也并非不安全。
不如“照本宣科”,带参数:
OleDbCommand STK = new OleDbCommand();
STK.Connection = conexao;
STK.CommandText = "SELECT * FROM tblCliente WHERE User like @userParameter";
STK.Parameters.AddWithValue("@userParameter", $"%{textBox1.Text}%")
Users.Clear();
var reader = STK.ExecuteReader();
while (reader.Read())
Users.Add(reader["User"].ToString());
看下面的代码。
此处使用using
运算符来释放资源——这一点很重要!
var dataSource = Path.Combine(
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
"Teste.accdb");
var builder = new OleDbConnectionStringBuilder();
builder.Provider = "Microsoft.ACE.OLEDB.12.0";
builder.DataSource = dataSource;
var connectionString = builder.ToString();
var sql = "SELECT ..."; // place your query here
using (var connection = new OleDbConnection(connectionString))
{
connection.Open();
using (var command = new OleDbCommand(sql, connection))
using (var reader = command.ExecuteReader())
{
var users = new List<User>();
while (reader.Read())
{
var user = new User();
user.ClientNumber = (int)reader["NºCliente"];
user.UserName = (string)reader["NomeUser"];
user.CodigoPostal = (string)reader["CodigoPostal"];
user.NIF = (string)reader["NIF"];
users.Add(user);
}
// return users; // Return data from method
}
}
此class用于存储用户数据。
将 属性 名称和类型更改为您需要的名称和类型。
class User
{
public int ClientNumber { get; set; }
public string UserName { get; set; }
public string CodigoPostal { get; set; }
public string NIF { get; set; }
}
当然,在 sql 查询中使用参数,如@dovid 在他的示例中所示。
感谢Alexander Petrov and dovid,解决了我的问题。但是我“找到”了解决方案并发送了。
OleDbConnection conexao = new OleDbConnection(string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= {0}\Teste.accdb", Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)));
OleDbCommand STK = new OleDbCommand("SELECT * FROM MyTable ORDER BY Id");
conexao.Open();
comando.Connection = STK;
var reader = comando.ExecuteReader();
var users = new List<User>();
while (reader.Read())
{
var user = new User();
user.ClientNumber = (reader["ClientNumber "]);
user.UserName= reader["UserName"];
user.CodigoPostal= reader["CodigoPostal"];
user.NIF= reader["NIF"];
users.Add(user);
}
class User
{
public string ClientNumber { get; set; }
public string UserName { get; set; }
public string CodigoPostal { get; set; }
public string NIF { get; set; }
}
我正在创建应用程序,但我有一个问题。
客户端在文本框中输入用户名,例如3个字母并在数据库(access)中搜索并添加数据库。
示例:用户:Rui。 并在数据库中搜索所有名称用户“Rui”。
//libraries
using Microsoft.VisualStudio.OLE.Interop;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
private void textBox1_TextChanged(object sender, EventArgs e)
{
OleDbConnection conexao = new OleDbConnection(string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= {0}\Teste.accdb", Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)));
List<string> Users = new List<string>();
OleDbCommand STK = new OleDbCommand($"SELECT NºCliente, NomeUser, CodigoPostal, NIF", conexao);
STK.CommandText = $" SELECT* FROM MyTable WHERE Str(Lista_Pokemon) like '*{textBox1.Text}*'";
User.Clear();
//this code is invention, probably is wrong
for(int d=0; d<Stk.Count()-1; d++)
User.Add(...);
}
如果你能帮助我谢谢。这个项目是c#,net framework,数据库是Access 2010。目前我没有创建class,但是如果你需要告诉我,我需要创建。
您需要创建一个 DbReader 并移动到下一行直到结束:
OleDbCommand STK = new OleDbCommand($"SELECT NºCliente, NomeUser, CodigoPostal, NIF", conexao);
STK.CommandText = $" SELECT * FROM MyTable WHERE Str(Lista_Pokemon) like '%{textBox1.Text}%'";
Users.Clear();
var reader = STK.ExecuteReader();
while (reader.Read())
Users.Add(reader["Lista_Pokemon"].ToString());
就安全性而言,将用户输入线程化到查询文本中被认为是一种危险的做法,而且在逻辑上也并非不安全。 不如“照本宣科”,带参数:
OleDbCommand STK = new OleDbCommand();
STK.Connection = conexao;
STK.CommandText = "SELECT * FROM tblCliente WHERE User like @userParameter";
STK.Parameters.AddWithValue("@userParameter", $"%{textBox1.Text}%")
Users.Clear();
var reader = STK.ExecuteReader();
while (reader.Read())
Users.Add(reader["User"].ToString());
看下面的代码。
此处使用using
运算符来释放资源——这一点很重要!
var dataSource = Path.Combine(
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
"Teste.accdb");
var builder = new OleDbConnectionStringBuilder();
builder.Provider = "Microsoft.ACE.OLEDB.12.0";
builder.DataSource = dataSource;
var connectionString = builder.ToString();
var sql = "SELECT ..."; // place your query here
using (var connection = new OleDbConnection(connectionString))
{
connection.Open();
using (var command = new OleDbCommand(sql, connection))
using (var reader = command.ExecuteReader())
{
var users = new List<User>();
while (reader.Read())
{
var user = new User();
user.ClientNumber = (int)reader["NºCliente"];
user.UserName = (string)reader["NomeUser"];
user.CodigoPostal = (string)reader["CodigoPostal"];
user.NIF = (string)reader["NIF"];
users.Add(user);
}
// return users; // Return data from method
}
}
此class用于存储用户数据。
将 属性 名称和类型更改为您需要的名称和类型。
class User
{
public int ClientNumber { get; set; }
public string UserName { get; set; }
public string CodigoPostal { get; set; }
public string NIF { get; set; }
}
当然,在 sql 查询中使用参数,如@dovid 在他的示例中所示。
感谢Alexander Petrov and dovid,解决了我的问题。但是我“找到”了解决方案并发送了。
OleDbConnection conexao = new OleDbConnection(string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= {0}\Teste.accdb", Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)));
OleDbCommand STK = new OleDbCommand("SELECT * FROM MyTable ORDER BY Id");
conexao.Open();
comando.Connection = STK;
var reader = comando.ExecuteReader();
var users = new List<User>();
while (reader.Read())
{
var user = new User();
user.ClientNumber = (reader["ClientNumber "]);
user.UserName= reader["UserName"];
user.CodigoPostal= reader["CodigoPostal"];
user.NIF= reader["NIF"];
users.Add(user);
}
class User
{
public string ClientNumber { get; set; }
public string UserName { get; set; }
public string CodigoPostal { get; set; }
public string NIF { get; set; }
}