我有一个简单的crud应用程序,我想按照他们的说法优雅地处理错误。
我只是没有尝试抓住。在我去C#学校之前,我开始学习一些python,但它并没有真正起作用。这是我的代码,
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Threading.Tasks;
namespace Crud
{
class Program
{
static string conn_string = "Data Source=MY_SQLSERVER;Initial Catalog=A_DATABASE";
private static void Create(string name, string email)
{
using (SqlConnection conn = new SqlConnection(conn_string))
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = @"INSERT INTO dbo.Person (name, email)
VALUES (@name, @email)";
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@email", email);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
private static void Read()
{
using (SqlConnection conn = new SqlConnection(conn_string))
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = @"SELECT id, name, email FROM dbo.Person ORDER BY id";
conn.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
Console.WriteLine("{0}\t{1}\t{2}", dr.GetInt32(0), dr.GetString(1), dr.GetString(2));
}
conn.Close();
}
}
private static void Update(int id, string name, string email)
{
using (SqlConnection conn = new SqlConnection(conn_string))
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = @"UPDATE dbo.Person SET name=@name, email=@email
WHERE id = @id";
cmd.Parameters.AddWithValue("@id", id);
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@email", email);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
private static void Delete(int id)
{
using (SqlConnection conn = new SqlConnection(conn_string))
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = @"DELETE FROM dbo.Person WHERE id=@id";
cmd.Parameters.AddWithValue("@id", id);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
static void Main(string[] args)
{
Console.WriteLine("Please Enter a Command (Create, Read, Update or Delete)");
string input = Console.ReadLine();
if (input == "Read" || input == "read")
{
Read();
Console.WriteLine("");
}
else if (input == "Create" || input == "create")
{
Console.WriteLine("Enter a name");
string name = Console.ReadLine();
Console.WriteLine("Enter an email address");
string email = Console.ReadLine();
Create(name, email);
// Assuming Create has not yet returned an error, everything successful
Console.WriteLine("Entry Created Successfully!");
}
else if (input == "Update" || input == "update")
{
int id;
bool check;
do
{
Console.WriteLine("What id would you like to change?");
string val = Console.ReadLine();
check = int.TryParse(val, out id);
} while (!check);
Console.WriteLine("Enter a name");
string name = Console.ReadLine();
Console.WriteLine("Enter an email address");
string email = Console.ReadLine();
Update(id, name, email);
Console.WriteLine("Entry Updated Successfully!");
}
else if (input == "Delete" || input == "delete")
{
int id;
bool check;
do
{
Console.WriteLine("What id would you like to delete?");
string val = Console.ReadLine();
check = int.TryParse(val, out id);
} while (!check);
Delete(id);
Console.WriteLine("Entry Deleted Successfully :(");
}
}
}
}
它有效,但它很脆弱。这是一个控制台应用程序,我有一天会变成一个HTML解决方案,但是......
我不确定将try catch块放在哪里,我不知道在哪里放置异常。然后我在msdn上,他们告诉我他们终于使用了。最后,我能理解的东西。所以,来自python我会说尝试显然是一样的,c sharp中的catch (){}
就像python中的except:
但是我没有在python中使用参数。我是个小伙子。我想我可以在.NET中使用Exception对象,但我不需要它。如果发生任何错误,我只想写一个自定义消息...我的意思是,写一个href有多难?我无法让它正常工作。
所以我的问题是,如果我想在每个crud操作中保留try catch块,我将如何构造Create
方法?
答案 0 :(得分:3)
基本C#异常处理:
try
{
//do stuff
}
catch
{
//write custom exception message
}
finally
{
//this is not necessary, but would be here
}
catch
块将捕获上例中的所有异常。如果你想对异常做一些事情,你可以这样做:
catch (Exception e)
{
//do something with e
}
在您的情况下,它看起来像:
private static void Create(string name, string email)
{
try
{
using (SqlConnection conn = new SqlConnection(conn_string))
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = @"INSERT INTO dbo.Person (name, email)
VALUES (@name, @email)";
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@email", email);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
catch
{
//print custom error message
}
}