Assume you have a table like this:
CREATE TABLE dbo.Users
(
Id INT NOT NULL,
UserName VARCHAR(1000) NOT NULL,
CONSTRAINT [PK_Users] PRIMARY KEY ([Id])
);
And assume you have a text file that looks like this (pipe delimited):
UserName|FirstName|LastName
testuser1|Test|User 1
testuser2|Test|User 2
testuser3|Test|User 3
The following code will search the table for each user in the file:
string commandText = "SELECT * FROM dbo.Users WHERE UserName = @userName;";
using (StreamReader streamReader = new StreamReader(@"C:\Users.txt"))
{
using (SqlConnection sqlConnection = new SqlConnection("data source=localhost;initial catalog=Scratch;integrated security=True;"))
{
using (SqlCommand sqlCommand = new SqlCommand(commandText, sqlConnection))
{
sqlConnection.Open();
do
{
var line = streamReader.ReadLine();
var userName = line.Split('|')[0];
sqlCommand.Parameters.AddWithValue("@userName", userName);
SqlDataReader dataReader = sqlCommand.ExecuteReader();
while (dataReader.Read())
{
}
sqlCommand.Parameters.Clear();
} while (!streamReader.EndOfStream);
sqlConnection.Close();
}
}
}
Keep in mind, this is one of a dozen ways to achieve what you are trying to do.