Deleting Multiple Rows from an Access Database - c#

I want to delete multiple records from access database using array.
The array is loaded dynamically from file names.
Then i query the database, and see if the database column values are matching with the array values, if not then delete it, if matches then do not delete it.
the problem is that:
Following is the code that deletes all records irrespective of the where in Condition.
arrays = Directory.GetFiles(sdira, "*", SearchOption.AllDirectories).Select(x => Path.GetFileName(x)).ToArray();
fnames.AddRange(arrays);
here I have use also for loop but that also didnt help me out :( like for(int u = 0; u < arrays.length; u++) { oledbcommand sqlcmd = new oledbcommand ("delete from table1 where name not in ("'+arrays[u]+"')",sqlconnection);
I am using this one currently foreach(string name in arrays)
{
OleDbCommand sqlcmd = new OleDbCommand("delete from table1 where name not in ('" + name + "')", sqlconnection);
sqlcmd.ExecuteNonQuery(); }`

One problem is that your code is confusing.
string [] a = {"" 'a.jpg', 'b.jpg', 'c.jpg' "}
First, you have double " in the beginning,should only be one.
string [] a = {" 'a.jpg', 'b.jpg', 'c.jpg' "}
Then this created a string array with one element,
a[0] = "'a.jpg', 'b.jpg', 'c.jpg'";
Then you do a foreach on this which natuarly ony executes once resulting in this query:
delete from table1 where name not in ('a.jpg', 'b.jpg', 'c.jpg')
But when you load the array dynamically you probably get this array
a[0] = 'a.jpg';
a[1] = 'b.jpg';
a[1] = 'c.jpg';
which will execute 3 times in the foreach resulting in the following 3 queries
delete from table1 where name not in ('a.jpg')
delete from table1 where name not in ('b.jpg')
delete from table1 where name not in ('c.jpg')
After the second one the table will be empty.
You should try this instead:
string[] names = { "a.jpg", "b.jpg","c.jpg","j.jpg" };
string allNames = "'" + String.Join("','", names) + "'";
OleDbCommand sqlcmd = new OleDbCommand("delete from table1 where name not in (" + allNames + ")", sqlconnection);
sqlcmd.ExecuteNonQuery();
Where names is created dynamically ofcause and this will result in the following query matching your test:
delete from table1 where name not in ('a.jpg', 'b.jpg', 'c.jpg')
My preferred way to dynamically fill an array is to use a list instead as a pure array is fixed in size and any change needs to create a new array.
You can loop over a list as eacy as an array.
List<string> names = new List<string>();
//or user var keyword
var names = new List<string>();
Then just use add method to add elements, loop this as needed.
names.Add(filename);
Then for the concatenation:
string allNames = "'" + String.Join("','", names.ToArray()) + "'";
And you are done.
Or you could use
string[] filePaths = Directory.GetFiles(#"c:\MyDir\", "*.jpg");
string[] names = filePaths.ToList().ConvertAll(n => n.Substring(n.LastIndexOf(#"\") + 1)).ToArray();

posting my comment as a answer
your string is not reading in 4 entries, its reading one entry of
string names = " 'a.jpg', 'b.jpg','c.jpg','j.jpg' ";
it should be
string[] names = { "a.jpg", "b.jpg","c.jpg","j.jpg" };
before your for each had a count of 1 now it should have a count of 4 with the actual values
Edit:
Not a lot of effort in this solution i must admit but if you want dynamic input could do something like:
string name = " 'a.jpg', 'b.jpg','c.jpg','j.jpg' ";
string[] names = name.Split(',').Select(x => x.Trim(' ').Trim('\'')).ToArray();
will update later if i get the change as the trims are not good atm
For populating it, if you want it as enumerable a example could be something like
IEnumerable<string> filelocations = Directory.GetFiles(sourceDirectory, "*", SearchOption.AllDirectories).Select(x => Path.GetFileName(x));
or for string array
string [] lok = Directory.GetFiles(sourceDirectory, "*", SearchOption.AllDirectories).Select(x => Path.GetFileName(x)).ToArray();

Sounds like you're either not reading the file correctly or the file is empty. You should be checking the array to make sure it's not empty before running the operation against the database. If the array is empty, it SHOULD delete everything in the database as there are no matches.

You need to define the array like this:
string[] names = { "a.jpg", "b.jpg", "c.jpg", "j.jpg" };
the way you are defining the array it contains only one value:
" 'a.jpg', 'b.jpg','c.jpg','j.jpg' "

The code in your comment will delete any files not matching the first file. that would be all non 'a.jpg' files. The next iteration will delete all files that don't match 'b.jpg' which would be 'a.jpg'. This results in an empty table. Edit: The array declaration you have generates a good IN clause when you do it manually, but when you're getting a list of filenames, you're not generating this same string.
You need to perform a join on the array to generate a single string for your where clause that way the where clause includes all files. Your ultimate where clause should look like:
where name not in ('a.jpg','b.jpg','c.jpg','d.jpg')
Right now you have:
where name not in ('a.jpg')
... next iteration
where name not in ('b.jpg')
Also, be mindful that IN operations are expensive and the longer the array is the faster the query grows.

Try using a list if you don't want to create a static sized array
List<string> names = new List<string>();
names.Add("a.jpg");
names.Add("b.jpg");
names.Add("c.jpg");
foreach (string name in names)
{
OleDbCommand sqlcmd = new OleDbCommand("delete from table1
where name not in (" + name + ")",
sqlconnection);
sqlcmd.ExecuteNonQuery();
}

Related

How to insert a ICollection<string> into one Field in ADO.NET?

I have ICollection object in my controller. This collection posted from view like this;
{string[3]}
[0]=a
[1]=b
[2]=c
public ActionResult TarifEkle(Tarifler tarif, ICollection<string> tAdim)
{
string mainconn = ConfigurationManager.ConnectionStrings["Tarif"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(mainconn);
string sqlquery = "insert into [NePisirsem].[dbo].[Tarifler](tAdim) values (#builder)";
SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
sqlconn.Open();
I change this value like this a,b,c with my builder;
var builder = new System.Text.StringBuilder();
foreach (string item in tAdim)
{
builder.Append(item).Append(",");
sqlcomm.Parameters.AddWithValue("#builder", tarif.tAdim);
}
It works values are successfully became i want (a,b,c) but I can't add this values this way a,b,c
sqlcomm.Parameters.AddWithValue("#builder", tarif.tAdim); this code takes always just first value a not all of them.
How can i do?
You need tho change the following section
var builder = new System.Text.StringBuilder();
foreach (string item in tAdim)
{
builder.Append(item).Append(",");
sqlcomm.Parameters.AddWithValue("#builder", tarif.tAdim);
}
to
sqlcomm.Parameters.AddWithValue("#builder", string.Join(",", tAdim));
#builder will then be replaced by a,b,c in your sqlquery if you need a, b, c replace "," with ", ". Read more about string.Join() here.
Don't use AddWithValue()
As you can read here, you should prefere Parameters.Add() over Parameters.AddWithValue() as you can specify the appropriate SqlDbType that your string is converted to.
sqlcomm.Parameters.Add("#builder", SqlDbType.VarChar, 30).Value = string.Join(",", tAdim);
Either use SqlDbType.VarChar or SqlDbType.NVarChar and replace 30 with your actual max lenght.
INSERT INTO Statement
As explained here, the format of an insert statement looks like this:
INSERT INTO table_name (column_list)
VALUES
(value_list_1),
(value_list_2),
...
(value_list_n);
column_list is a list of Columns that in your case are on [NePisirsem].[dbo].[Tarifler] e.g. tAdim. Then multiple value_lists can be supplied, each generating a new row in your table. value_list needs to match the order of column_list

Selecting all rows that match parts of a string

I have a table of people
Id | Name | Info
1 | Bob | some info
2 | Mark | some info
And I have a list of names in a string separated by commas which looks like:
"Mark, Bob, John"
I need an SQL command that would select all rows that match the names in the list.
Any idea how to do it?
It's in c# on wpf and the database is PostgreSQL if that matters.
In general case, you can try building parametrized query:
string names = "Mark, Bob, John";
string[] filters = names
.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(name => name.Trim())
.Where(name => !string.IsNullOrEmpty(name))
.ToArray();
//TODO: Put the right class instead of SqlConnection
using (var connection = new SqlConnection("ConnectionStringHere")) {
connection.Open();
using (var command = connection.CreateCommand()) {
command.Connection = connection;
command.CommandText =
$#"select *
from MyTable
where Name in ({string.Join(", ", filters.Select((name, i) => $"#prm_Name{i}"))})";
//TODO: Change AddWithValue into Add and provide the RDBMS type
for (int i = 0; i < filters.Length; ++i)
command.Parameters.AddWithValue($"#prm_Name{i}", filters[i]);
using (var reader = command.ExecuteReader()) {
...
}
}
}
create an ad-hoc query from your name string like:
string Names = "Mark, Bob, John";
//Step 1: add quotes to Names
var names = Names.Split(',').Select(x => $"'{x}'").ToList();
//Step 2: Join Quoted Names
var result = String.Join(",", names.ToArray());
//Step 3 Create Ad hoc query
string query = $"SELECT * FROM people WHERE NAME IN ({result})";
I need an SQL command that would select all rows that match the names in the list.
How about IN?
SELECT * FROM People WHERE Name IN ('Mark', 'Bob', 'John')
You should take the string apart with C# first. There are many different ways to do the loop, but after you substring the names, place them in an array or a list or something. Within the loop, look for the names using the LIKE clause.
'SELECT *
FROM myTable
WHERE LOWER(nameField) LIKE LOWER(%' + variable + '%)';
Not sure how you're building your SQL but if you are using a string it would look kind of like this. You can take out the LIKE if you're only looking for direct matches. Its better to place all values in either lowercase or uppercase to make sure you're getting them UNLESS you know FOR SURE you want Mark not mark or MARK etc. I do not know C#, so I am sure there's a much better way, with adding parts of an array together with commas and such to use the IN operator.

Adding multiple values to a SQL Query in C#

public IHttpActionResult Post(ModelName data){
var query="Select * from Table where ColumnName ='"+data.ID+"'";
return Ok(Value);
}
In the above SQL query I want to add multiple ID's in data.ID. for an example I in one instance i need to get data for 2 ID's. in another time I need data from 20 ID's. Im using angular js to pass the json array with data. How can I do that. Do I have to write many data.Id1, data.id2 etc for all my Id data. Any help
As long as you are sanitising your inputs you can use:
var query = "Select * from Table where ColumnName in (" + list_of_ids + ")";
This will work whether you have one Id or several.
list_of_ids will have to be a comma delimited string of values:
"a, b, c, d"
so you'll need some conversion method for adding each id to the list.
You could build up an list of values:
var myList = new List<string>();
if (data.Id1 is set)
{
myList.Add(data.Id1.ToString());
}
etc.
string list_of_ids = string.Join(",", myList);

How to avoid the single quote within a column value

I have the following code which iterates through a CSV file and writes to the SQL table:
foreach (string line in lines.Skip(1))
{
var sqlfmt = "INSERT INTO [" + tab + "] values ({0})";
var insert = string.Format(sqlfmt, line.Replace("\"", "'"));
MessageBox.Show(insert + "");
SqlCommand ty = new SqlCommand(insert, myConnection);
ty.ExecuteNonQuery();
}
The issue I have now is if one of the column has ' in the text my application crashes.
What would be the best way to avoid the issue?
Just change
var insert = string.Format(sqlfmt, line.Replace("\"", "'"));
To:
var insert = string.Format(sqlfmt, line.Replace("'", "''").Replace("\"", "'"));
The reason for this is that in T-SQL (SQL Server's version of SQL), single-quotes inside a string are escaped by another single quote. For example, if you wanted to properly quote the string Bob's quotes', the properly-escaped SQL string would be 'Bob''s quotes'''.

Add values to a table cell using C#

I want to add multiple values to a table cell separated by commas, the table I got is
Id ,Name , Type . I want to add multiple names in the name column, so the row will be something like:
ID Name Type
1 Peter, Jas , Roden , Karen Class A
I have done simple insertion which is:
[WebMethod]
public static string Insertion(string Name)
{
//List<string> result = new List<string>();
SqlConnection con = new SqlConnection("Data Source=XXX;Initial Catalog=XXX;User ID=sa;Password=XXXXX");
{
string query = "Insert into TestTable values #Name";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.Add("#Name", SqlDbType.NVarChar).Value = Name;
{
con.Open();
cmd.ExecuteNonQuery();
//test();
con.Close();
return "True";
}
}
}
Can any one guide me on how to add multiple names which should be separated by commas, also I need to make sure there is no duplication of names.
you can send parameter value as "Peter, Jas , Roden , Karen" then it will insert that text in to given record.
but if you have array or List of names to be added, you can easily create the insert string as below
var names = string.Join("," ,namesArray.Distinct());
now you can call the service method using above generated names string
Can any one guide me on how to add multiple names which should be separated by commas
You need to use parameterized queries. It will also help you inb avoiding sql injection attacks.
like
cmd.Parameters.AddWithValue("#name",name);
also i need to make sure there is no duplication of names
Something like this should work for you
string name = "a,b,c,d,a,b,c,d";
HashSet<string> h = new HashSet<string>(name.Split(','));
string distinctNames = string.Join(",", h);

Categories