I've recently explored c# to myself. But stuck with this problem.
So I have a method dbExec
public void dbExec(Action<OleDbCommand> func)
{
using (var conn = new OleDbConnection(connStr))
{
conn.Open();
var cmd = conn.CreateCommand();
func(cmd);
}
}
delegate,
public delegate void DelCmd(OleDbCommand cmd);
and another method:
public ICollection<string> CheckUserPermissions()
{
List<string> logins = new List<string>();
DelCmd delCmd = delegate(OleDbCommand cmd)
{
cmd.CommandText = "SELECT PERMISSIONS.LOGIN FROM PERMISSIONS";
using (var rdr = cmd.ExecuteReader()) while (rdr.Read()) logins.Add(rdr["LOGIN"].ToString());
};
dbExec(delcmd);
return logins;
}
The problem with dbExec(delcmd); statement. The error is "delcmd doesn't exist in current context". How to pass an anonymous method as a parameter to another method with Action declared parameter?
You could also avoid defining a delegate altogether.
Like this:
public ICollection<string> CheckUserPermissions()
{
List<string> logins = new List<string>();
Action<OleDbCommand> delCmd = cmd =>
{
cmd.CommandText = "SELECT PERMISSIONS.LOGIN FROM PERMISSIONS";
using (var rdr = cmd.ExecuteReader())
while (rdr.Read()) logins.Add(rdr["LOGIN"].ToString());
};
dbExec(delCmd);
return logins;
}
Edit: I actually mean what Servy wrote in the comment on the other answer, but he described it way better.
You have a typo - it should be delCmd instead of delcmd. C# is a case-sensitive language
UPDATE: DelCmd is not same as Action<OleDbCommand> - that is different types, and you even can't cast delegates to each other. But you can create new action delegate:
dbExec(new Action<OleDbCommand>(delCmd));
Related
I have two methods that have similar input parameter types, but the parameters themselves are different and used to build an SQL statement within the method.
C# doesn't like this - "Type Database already defines a member called 'DatabaseSearch' with the same parameter types."
As a newbie, this sounds to me like I'm structuring the class or methods wrong?
Should I perhaps be building the SQL statement outside the method and passing it and its parameters in?
// Surname ONLY
public void DatabaseSearch(DataGrid DataGrid, string surname)
{
string database_file_path = #"Data Source=.\MemberDB.db";
string sqlCmd = "Select * FROM Members WHERE Surname = #surname";
using (var con = new SQLiteConnection(database_file_path))
{
using (var cmd = new SQLiteCommand(con))
{
con.Open();
cmd.Parameters.AddWithValue("#surname", surname);
cmd.CommandText = sqlCmd;
var dataAdapter = new SQLiteDataAdapter(cmd);
var dt = new DataTable("Members");
dataAdapter.Fill(dt);
DataGrid.ItemsSource = dt.DefaultView;
dataAdapter.Update(dt);
}
}
}
// Firstname ONLY
public void DatabaseSearch(DataGrid DataGrid, string firstname)
{
string database_file_path = #"Data Source=.\MemberDB.db";
string sqlCmd = "Select * FROM Members WHERE FirstName = #firstname";
using (var con = new SQLiteConnection(database_file_path))
{
using (var cmd = new SQLiteCommand(con))
{
con.Open();
cmd.Parameters.AddWithValue("#firstname", firstname);
cmd.CommandText = sqlCmd;
var dataAdapter = new SQLiteDataAdapter(cmd);
var dt = new DataTable("Members");
dataAdapter.Fill(dt);
DataGrid.ItemsSource = dt.DefaultView;
dataAdapter.Update(dt);
}
}
}
Summary:
How do you have two overloaded methods with the same types of parameters?
public void myMethod( int one, string one){
....some stuff done...
}
public void myMethod( int two, string two){
....different stuff done...
}
You can either change the method name or change the order of the parameters. The first approach is better.
public void DatabaseSearchByFirstName(int one, string one){
// ...some stuff done...
}
public void DatabaseSearchBySurName(int two, string two){
// ...different stuff done...
}
In the code below I am trying to pass a function func in to the GetData function. This would take the reader object and map it to a generic object.
I was hoping to pass GetData an object type along with a function to map data to that object type so I didn't have to repeatedly open / close / dispose the connection.
Is this possible or does anyone have any alternative suggestions?
public T GetData<T>(string cmdText,Func<T> func)
{
using (SqlConnection conn = new SqlConnection(connectionStringBuilder.ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
SqlDataReader reader = cmd.ExecuteReader();
//return func(reader);
// WITHIN THE FUNC FUNCTION:
// while (reader.Read())
// {
// Map function to T e.g
// T.property = reader["column"];
// Return T
// }
}
}
}
The signature that you're looking for is this:
T GetData<T>(string cmdText, Func<SqlDataReader, T> func)
Then you can go ahead and write your function as this:
public T GetData<T>(string cmdText, Func<SqlDataReader, T> func)
{
using (var conn = new SqlConnection(connectionStringBuilder.ConnectionString))
{
using (var cmd = new SqlCommand(cmdText, conn))
{
var reader = cmd.ExecuteReader();
return func(reader);
}
}
}
And you would use it like this:
var result = GetData("select * from Foo", dr =>
{
while (dr.Read())
{
return new { property = dr["column"] };
}
throw new DataException();
});
Now that's based on how you said you'd like to use it in your question.
However, you've made the use of the function a bit hard on yourself as you've split the implementation - part is in GetData and part is in the calling code.
You're better off using this signature:
IEnumerable<T> GetData<T>(string cmdText, Func<SqlDataReader, T> func)
Now you can write the method like this:
public IEnumerable<T> GetData<T>(string cmdText, Func<SqlDataReader, T> func)
{
using (var conn = new SqlConnection(connectionStringBuilder.ConnectionString))
{
using (var cmd = new SqlCommand(cmdText, conn))
{
var reader = cmd.ExecuteReader();
while (reader.Read())
{
yield return func(reader);
}
}
}
}
The advantage now is that the calling code is much simpler:
var results = GetData("select * from Foo", dr => new { property = dr["column"] });
This returns as many rows of data as your query returns.
If you know that your calling code only returns a single value, then you can drop a .Single() at the end of the method call to ensure you get one and only one result.
I know how to pass one parameter to an sql query but i want to create a function to pass multiple params that will have differents type and here im stuck.
public List<T> RawSql<T>(string query, params object[] parameters)
{
var command = context.Database.GetDbConnection().CreateCommand();
command.CommandText = query;
command.CommandType = CommandType.Text;
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "#bookId";
parameter.SqlDbType = SqlDbType.Int;
parameter.Value = parameters[0];
command.Parameters.Add(parameter);
var result = command.ExecuteReader())
return result;
}
Usage :
var rows = helper.RawSql("myStoreProc #bookId", x=> new Book { Id = (bool)x[0] }, bookId);
But how i can change the RawSql function to pass multiple parameters like this :
var rows = helper.RawSql("myStoreProc #bookId, #authorName", x=> new Book { Id = (bool)x[0] }, bookId, authorName);
I would also suggest using Dapper instead of reinventing the wheel - but if you can't for some reason, I would change the method signature to accept params SqlParameter[] parameters instead of params object[] parameters - and then all you need to do in the method is command.Parameters.AddRange(parameters);.
As Marc Gravel wrote in his comment - naming the parameters is going to be the biggest problem if you are simply using object[].
Here is a method I wrote to compare values from two different days:
public DataTable sqlToDTCompare(string conStr, string stpName, DateTime startDate, DateTime endDate, int percent)
{
//receives connection string and stored procedure name
//then returns populated data table
DataTable dt = new DataTable();
using (var con = new SqlConnection(conStr))
using (var cmd = new SqlCommand(stpName, con))
using (var da = new SqlDataAdapter(cmd))
{
cmd.Parameters.Add("#StartDate", SqlDbType.Date).Value = startDate;
cmd.Parameters.Add("#EndDate", SqlDbType.Date).Value = endDate;
cmd.Parameters.Add("#Percent", SqlDbType.Int).Value = percent;
cmd.CommandType = CommandType.StoredProcedure;
da.Fill(dt);
}
return dt;
}
This method then returns that data to a DataTable (was what I needed at time of writing). You would be able to use this , with modifying to be of better fit for your needs.
What you're looking to use is something along:
SqlCommand.Parameters.Add("#Param1", SqlDbType.Type).Value = param1;
SqlCommand.Parameters.Add("#Param2", SqlDbType.Type).Value = param2;
SqlCommand.Parameters.Add("#Param3", SqlDbType.Type).Value = param3;
.....
Where .Type in SqlDbType.Type can be changed to matche whatever SQL datatype you're needing (ex. SqlDbType.Date).
I have previously done implementations along these lines.
public IEnumerable<SampleModel> RetrieveSampleByFilter(string query, params SqlParameter[] parameters)
{
using(var connection = new SqlConnection(dbConnection))
using(var command = new SqlCommand(query, connection))
{
connection.Open();
if(parameters.Length > 0)
foreach(var parameter in parameters)
command.Parameters.Add(parameter);
// Could also do, instead of loop:
// command.Parameters.AddRange(parameters);
using(var reader = command.ExecuteReader())
while(reader != null)
yield return new Sample()
{
Id = reader["Id"],
...
}
}
}
I actually wrote an extension method to read the values returned back into my object, but this allows you to pass a query and a series of parameters to simply return your object.
I would look into Dapper, saves a lot of time. But I find the problem with trying to reuse with the above type of solution creates a bit of tightly coupling often.
By doing this approach you push specific information about your query elsewhere, which separates logic directly out of the repository and tightly couples to another dependency and knowledge.
Hi i want to know if there is a more simplified method than the one im using now to find if there is an entry or not in a mysql db.
public static bool check_db_entry(string query)
{
using (var conn = new MySqlConnection(DbMethods.constr))
{
using (var cmd = new MySqlCommand(query, conn))
{
conn.Open();
using (var rdr = cmd.ExecuteReader())
{
if (rdr.Read() == false)
{
return false;
}
else
{
return true;
}
}
}
}
}
Leaving aside for now that fact that methods that accept only sql strings are inherently unsafe, this smells wrong to me. If you're going to have public methods that accept arbitrary sql commands (remember: I said "If"), then undoubtedly you have one that returns the data directly. You should rely on that as your base. Here's an example:
private static IEnumerable<IDataRecord> GetDataImpl(string query)
{
using (var conn = new MySqlConnection(DbMethods.constr))
using (var cmd = new MySqlCommand(query, conn))
{
conn.Open();
using (var rdr = cmd.ExecuteReader())
{
yield return rdr;
}
}
}
public static bool check_db_entry(string query)
{
return GetDataImpl(query).Any();
}
Note there is a reason I listed the first method as private. As written, it can have weird side effects if you don't first copy each element in the reader before returning it to a higher abstraction level in your program. But you can get the public version easily enough:
public static IEnumerable<T> GetData<T>(string query, Func<IDataRecord,T> copy)
{
return GetDataImpl(query).Select(copy);
}
Taking aside your design issues pointed in the question's comments, if you want to check the existence an entry in the database, you should always query with COUNT(*): SELECT COUNT(*) FROM yourTable [WHERE theCondition].
If that is all you pass to your function, you can then simply with:
public static bool check_db_entry(string query)
{
using (var conn = new MySqlConnection(DbMethods.constr))
{
conn.Open();
using (var cmd = new MySqlCommand(query, conn))
{
return (int)cmd.ExecuteScalar() == 1;
}
}
}
And if you want to streamline it:
public static bool check_db_entry(string query)
{
using (var conn = new MySqlConnection(DbMethods.constr))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = query;
return (int)cmd.ExecuteScalar() == 1;
}
}
when you select items from the listbox, you want to delete selecteditems. Why doesnt it work when selected data removed from database? I must have missed something. I got error message
No mapping exists from object type.
This is a method parameter:
IsDelete = _dinnerRemover.RemoveDinners(lstDinner.SelectedItems);
This class is to delete data from database
public bool RemoveDinners(dynamic dinnerItems)
{
Dinners = new List<FoodInformation>();
using (var sqlConn = new SqlConnection(_sqlConnectionString))
{
const string sqlQuery = "delete from DinnerTemplates where Dinner = #dinner";
using (var command = new SqlCommand(sqlQuery, sqlConn))
{
try
{
//command.CommandType = CommandType.StoredProcedure;
//command.CommandText = "sp_dinner";
foreach (var item in dinnerItems)
{
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("#dinner", item);
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sqlConn.Close();
}
}
}
return Dinners;
}
If dinnerItems is a list of strings then say that, don't use dynamic unless you absolutely have to.
To delete a bunch of items, issue one sql query with an IN clause. Don't issue lots of individual queries.
Try this:
public int RemoveDinners(List<string> dinnerItems)
{
using (var sqlConn = new SqlConnection(_sqlConnectionString))
{
const string sqlQuery = "delete from DinnerTemplates where Dinner in ({0})";
using (var command = new SqlCommand())
{
var paramNames = new string[dinnerItems.Count];
int i = 0;
foreach (string item in dinnerItems)
{
string paramName = "#Dinner" + i;
command.Parameters.AddWithValue(paramName, item);
paramNames[i] = paramName;
i += 1;
}
command.CommandText = String.Format(sqlQuery, String.Join(",", paramNames));
command.Connection = sqlConn;
command.CommandType = CommandType.Text;
sqlConn.Open();
return command.ExecuteNonQuery();
}
}
}
You have to bear in mind that you kind of left out some really relevant code, like what is a DinnerItem, since you're getting the error on a line related to its type.
However, the reason you're getting that error is because item can't be marshaled to a type of something like string or int.
That's probably because item is likely a custom class. One option would be to override the ToString method of the class:
public override string ToString() {
// return some property value, or set of property values
// strung together here.
}
another option would be to send in the actual Property you want off of item when issuing AddWithValue.
You need to define SqlDbType for command's parameter.
don't use dynamic type,use string..
if i were you,i would rather
IsDelete = _dinnerRemover.RemoveDinners(lstDinner.SelectedItems.ToString());
change the parameter to :
public bool RemoveDinners(string dinnerItems)
and the query to :
const string sqlQuery = "delete from DinnerTemplates where Dinner = dinnerItems";