Лучший отвечающий
Подскажите, как поправить в код! что бы пропала ошибка?

Вопрос
-
public string GetConfigParamByName(string paramName) { string res = ""; con.Open(); com = new OleDbCommand("Select Value from Configurations where ParamName = '" + paramName + "'", con); reader = com.ExecuteReader(); if (reader) { res = reader.GetString(0); } con.Close(); return res; }
Ошибка: Ошибка1 Неявное преобразование типа "System.Data.OleDb.OleDbDataReader" в "bool" невозможно
Как ёё испарвить??
- Изменено Иван Лукашов 2 апреля 2013 г. 12:27
2 апреля 2013 г. 12:25
Ответы
-
When you're going to stop this bad practice?
public string GetConfigParamByName(string paramName) { string res = ""; con.Open(); string sql = "Select Value from Configurations where ParamName = ?"; com = new OleDbCommand(sql, con); com.Parameters.Add("@paramName", OleDbType.Char, 30).Value = paramName; reader = com.ExecuteReader(); while (reader.Read()) // the error was here { res = reader.GetString(0); } con.Close(); return res; }
For every expert, there is an equal and opposite expert. - Becker's Law
My blog- Изменено Naomi N 2 апреля 2013 г. 12:56
- Помечено в качестве ответа Иван Лукашов 2 апреля 2013 г. 13:16
2 апреля 2013 г. 12:56
Все ответы
-
When you're going to stop this bad practice?
public string GetConfigParamByName(string paramName) { string res = ""; con.Open(); string sql = "Select Value from Configurations where ParamName = ?"; com = new OleDbCommand(sql, con); com.Parameters.Add("@paramName", OleDbType.Char, 30).Value = paramName; reader = com.ExecuteReader(); while (reader.Read()) // the error was here { res = reader.GetString(0); } con.Close(); return res; }
For every expert, there is an equal and opposite expert. - Becker's Law
My blog- Изменено Naomi N 2 апреля 2013 г. 12:56
- Помечено в качестве ответа Иван Лукашов 2 апреля 2013 г. 13:16
2 апреля 2013 г. 12:56 -
When you're going to stop this bad practice?
public string GetConfigParamByName(string paramName) { string res = ""; con.Open(); string sql = "Select Value from Configurations where ParamName = ?"; com = new OleDbCommand(sql, con); com.Parameters.Add("@paramName", OleDbType.Char, 30).Value = paramName; reader = com.ExecuteReader(); while (reader.Read()) // the error was here { res = reader.GetString(0); } con.Close(); return res; }
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
2 апреля 2013 г. 13:20 -
I meant - stop embedding parameters into the SQL string using string concatenation. Start to always use parameters and if you see code when you're not doing it, take a moment and fix it.
For every expert, there is an equal and opposite expert. - Becker's Law
My blog2 апреля 2013 г. 15:08