using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
BookModel model = new BookModel();
Console.WriteLine("请输入书籍类别号");
model.FName = Console.ReadLine();
Console.WriteLine("请输入书籍类别号");
model.FCategoryID = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入作者名称");
model.FAuthor = Console.ReadLine();
Console.WriteLine("请输入出版日期");
model.FYearPublished = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入是否删除(false/ture)");
model.IsDel = Convert.ToBoolean(Console.ReadLine());
int rel = new Program().Add(model);//因为静态方法不能直接调用非静态方法,所以要实例化一个新的对象进行调用
List<BookModel> list = new List<BookModel>();
list = (List<BookModel>)(new Program().Table());
foreach (BookModel m in list)
{
Console.WriteLine(m.FAuthor.ToString());
Console.WriteLine(m.FCategoryID.ToString());
Console.WriteLine(m.FId.ToString());
Console.WriteLine(m.FName.ToCharArray());
Console.WriteLine(m.FYearPublished.ToString());
Console.WriteLine(m.IsDel.ToString());
}
Console.ReadKey();
}
/// <summary>
/// 添加数据
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public int Add(BookModel model)
{
string sqlText = "insert into T_Book(FAuthor,FCategoryID,FName,FYearPublished,IsDel)values(@FAuthor,@FCategoryID,@FName,@FYearPublished,@IsDel);select @@identity";
object rel = SqlHelper.ExecuteScalar(sqlText, new SqlParameter("FAuthor", ModelItemIsNow(model.FAuthor)),
new SqlParameter("FCategoryID", ModelItemIsNow(model.FCategoryID)),
new SqlParameter("FName", ModelItemIsNow(model.FName)),
new SqlParameter("FYearPublished", ModelItemIsNow(model.FYearPublished)),
new SqlParameter("IsDel", ModelItemIsNow(model.IsDel)));
return Convert.ToInt32(rel);
}
public IEnumerable<BookModel> Table()
{
string sqlText = "select * from T_Book";
DataTable tb = new DataTable();
tb = SqlHelper.ExecuteDataTable(sqlText);
List<BookModel> list = new List<BookModel>();
foreach (DataRow row in tb.Rows)
{
BookModel model = new BookModel();
model.FAuthor = row.IsNull("FAuthor") ? null : (string)row["FAuthor"];
model.FCategoryID = row.IsNull("FCategoryID") ? null : (int?)row["FCategoryID"];
model.FId = (int)row["FId"];//因为FID是主健字段,所以一定是非空字段
model.FName = row.IsNull("FName") ? null : (string)row["FName"];
model.FYearPublished = row.IsNull("FYearPublished") ? null : (int?)row["FYearPublished"];
model.IsDel = row.IsNull("IsDel") ? null : (bool?)row["IsDel"];
list.Add(model);
}
return list;
}
/// <summary>
/// 判断传递的参数是否为空,若为空,返回DBNULL的值
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public object ModelItemIsNow(object str)
{
if (str == null || str.ToString().Trim ().Length <= 0)
{
return DBNull.Value;
}
else
{
return str;
}
}
}
class SqlHelper
{
private static readonly string conStr = "Data Source=.;Initial Catalog=one;Persist Security Info=True;User ID=sa;Password=liuxianbin123";
private static SqlConnection con;
/// <summary>
/// 创建数据库连接
/// </summary>
public static SqlConnection Con
{
get
{
if (con == null || con.State == ConnectionState.Broken)
{
con = new SqlConnection(conStr);
}
return con;
}
}
public static DataTable ExecuteDataTable(string sqlText, params SqlParameter[] parameter)
{
SqlCommand com = new SqlCommand(sqlText, Con);
= CommandType.Text;
if (parameter != null || parameter.Length > 0)
{
com.Parameters.AddRange(parameter);
}
SqlDataAdapter ad = new SqlDataAdapter(com);
DataTable tb = new DataTable();
try
{
ad.Fill(tb);
return tb;
}
catch (Exception ex)
{
throw ex;
}
}
public static object ExecuteScalar(string sqlText, params SqlParameter[] parameter)
{
Con.Open();
SqlCommand com = new SqlCommand(sqlText, Con);
if (parameter.Length > 0 || parameter != null)
{
com.Parameters.AddRange(parameter);
}
try
{
return com.ExecuteScalar();
}
catch (Exception ex)
{
throw ex;
}
finally
{
Con.Close();
}
}
public static int ExecuteNonQury(string sqlText, params SqlParameter[] parameter)
{
Con.Open();
SqlCommand com = new SqlCommand(sqlText, Con);
if (parameter.Length > 0 || parameter != null)
{
com.Parameters.AddRange(parameter);
}
try
{
return com.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
Con.Close();
}
}
}
class BookModel
{
private int fId;
public int FId
{
get { return fId; }
set { fId = value; }
}
private string fName;
public string FName
{
get { return fName; }
set { fName = value; }
}
private int? fYearPublished;
public int? FYearPublished
{
get { return fYearPublished; }
set { fYearPublished = value; }
}
private int? fCategoryID;
public int? FCategoryID
{
get { return fCategoryID; }
set { fCategoryID = value; }
}
private bool? isDel;
public bool? IsDel
{
get { return isDel; }
set { isDel = value; }
}
private string fAuthor;
public string FAuthor
{
get { return fAuthor; }
set { fAuthor = value; }
}
}
}
没有修改跟删除,这俩功能你自己写吧,很简单