Найти ошибки в коде - C# (181654)
Формулировка задачи:
Выбивает 3 ошибки:
1)Неявное преобразование типа "string" в "bool" невозможно
2)Левая часть выражения присваивания должна быть переменной, свойством или индексатором
3)Элемент "CommandText" не существует в текущем контексте.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using System.Data.Common;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string conString = @"Provider=Microsoft.ACE.OLEDB.10.0; DataSource=DataBase1.accdb";
OleDbConnection conn = new OleDbConnection(conString);
DbCommand comm;
DbDataReader dr;
conn.Open();
if (conn.State.ToString() = "Open")
Console.WriteLine("Связь з БД установлена");
comm = conn.CreateCommand();
comm.CommandText = "INSERT INTO naputku(Kind, Name, Extent, Price, Components) VALUES ('негазированные', 'Сок', '1', '24,90', 'яблоко, сахар, вода');";
int rows = comm.ExecuteNonQuery();
if (rows != 0) Console.WriteLine("вы успешно добавили данные\n");
DataTable dt = new DataTable();
comm = conn.CreateCommand();
comm = CommandText = "Select * from Naputku";
dr = comm.ExecuteReader();
dt.Load(dr);
foreach (DataRow row in dt.Rows)
{ Console.WriteLine(row[0] + " " + row[1] + " " + row[2] + " " + row[3] + " " + row[4] + " " + row[5]); }
comm = conn.CreateCommand();
comm.CommandText = "Select count(*) from Naputku";
int count = (int)comm.ExecuteScalar();
Console.WriteLine("\n В БД:" + count + "записей.");
conn.Close();
Console.WriteLine("Связь з БД завершен.");
Console.ReadKey();
}
}
}Решение задачи: «Найти ошибки в коде»
textual
Листинг программы
if (conn.State.ToString() == "Open")
Console.WriteLine("Связь з БД установлена");
comm.CommandText = "INSERT INTO naputku(Kind, Name, Extent, Price, Components) VALUES ('негазированные', 'Сок', '1', '24,90', 'яблоко, сахар, вода');";