Вопрос пользователя:
Ответ нейросети:
using System; using System.Data.SqlClient;
namespace VegetableAndFruitDatabase { class Program { static void Main(string[] args) { string connectionString = "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=VegetableAndFruitDB;Integrated Security=True"; SqlConnection connection = new SqlConnection(connectionString);
try
{
connection.Open();
Console.WriteLine("Connection to database successful.");
// Create table
string createTableQuery = "CREATE TABLE VegetablesAndFruits (Id INT PRIMARY KEY IDENTITY, Name NVARCHAR(50), Type NVARCHAR(10), Color NVARCHAR(20), Calories INT)";
SqlCommand createTableCommand = new SqlCommand(createTableQuery, connection);
createTableCommand.ExecuteNonQuery();
Console.WriteLine("Table created successfully.");
// Insert data
string insertDataQuery = "INSERT INTO VegetablesAndFruits (Name, Type, Color, Calories) VALUES ('Apple', 'Fruit', 'Red', 52), ('Carrot', 'Vegetable', 'Orange', 41), ('Banana', 'Fruit', 'Yellow', 89)";
SqlCommand insertDataCommand = new SqlCommand(insertDataQuery, connection);
insertDataCommand.ExecuteNonQuery();
Console.WriteLine("Data inserted successfully.");
// Display data
string selectDataQuery = "SELECT * FROM VegetablesAndFruits";
SqlCommand selectDataCommand = new SqlCommand(selectDataQuery, connection);
SqlDataReader reader = selectDataCommand.ExecuteReader();
while (reader.Read())
{
Console.WriteLine($"Id: {reader["Id"]}, Name: {reader["Name"]}, Type: {reader["Type"]}, Color: {reader["Color"]}, Calories: {reader["Calories"]}");
}
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
finally
{
connection.Close();
Console.WriteLine("Connection closed.");
}
}
}
}
Напишите нам, если в вопросе есть ваши персональные данные ([email protected])
Обратите внимание: ответы, предоставляемые искусственным интеллектом, могут не всегда быть точными. Не рассчитывайте на них в критически важных областях, таких как медицина, юриспруденция, финансы или в вопросах, связанных с безопасностью. Для важных решений всегда обращайтесь к квалифицированным специалистам. Администрация сайта не несет ответственности за контент, сгенерированный автоматически.