2012-04-16 59 views
0

当我从表中的列中获取值时,我的项目中有一个项目。我通过C#代码创建数据库。无法从表中的列中获取数据当值“”

这是我的代码:

public static int typeTransactionIncome = 1; // thu 
    public static int typeTransactionExpense = 2; // chi 
    public static int typeTransactionDebt = 3; // nợ 
    public static int typeTransactionLoan = 4; // cho vay 

    public static string tableNameCategory = "categories"; 

    private static string createTableTransactionString = "CREATE TABLE IF NOT EXISTS \"transactions\" " + "(\"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ," 
     + "\"name\" VARCHAR(140) ," + "\"amount\" FLOAT NOT NULL DEFAULT (0) ," + "\"type\" INTEGER NOT NULL ," 
     + "\"created_date\" DATETIME NOT NULL DEFAULT (CURRENT_DATE) ," + "\"displayed_date\" DATETIME NOT NULL DEFAULT (CURRENT_DATE) ," 
     + "\"cat_id\" INTEGER NOT NULL DEFAULT (0) ," + "\"with_person\" VARCHAR(50)," + "\"remind_date\" DATETIME," 
     + "\"remind_num\" INTEGER DEFAULT (0) ," + "\"note\" VARCHAR(140) ," + "\"status\" BOOL NOT NULL DEFAULT (0)," 
     + "\"user_id\" INT NOT NULL DEFAULT (1))"; 

这是插入代码:插入某些列,并有2插入menthod

public void insertNewIncome(string _name, string _note, double _amount, DateTime _displayedDate, int _catId, int _usertId) 
    { 
     try 
     { 
      openConnection(); 

      SqliteCommand cmd = conn.CreateCommand(); 
      cmd.CommandText = "INSERT INTO " + tableNameTransaction + "(type, name, note, amount, displayed_date, cat_id, user_id) values (@type, @name, @note, @amount, @displayed_date, @cat_id, @user_id)"; 
      cmd.Parameters.Add("@type", typeTransactionIncome); 
      cmd.Parameters.Add("@name", _name); 
      cmd.Parameters.Add("@note", _note); 
      cmd.Parameters.Add("@amount", _amount); 
      cmd.Parameters.Add("@displayed_date", _displayedDate); 
      cmd.Parameters.Add("@cat_id", _catId); 
      cmd.Parameters.Add("@user_id", _usertId); 
      cmd.ExecuteNonQuery(); 
      //MessageBox.Show("income"); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
     finally 
     { 
      closeConnection(); 
     } 
    } 

public void insertNewDebt(string _debter, string _note, double _amount, DateTime _ngay_vay, DateTime _ngay_tra, int _user_id) 
    { 
     try 
     { 
      openConnection(); 

      SqliteCommand cmd = conn.CreateCommand(); 
      cmd.CommandText = "INSERT INTO " + tableNameTransaction + "(type, with_person, note, amount, displayed_date, remind_date, user_id) VALUES (@type, @with_person, @note, @amount, @displayed_date, @remind_date, @user_id)"; 
      cmd.Parameters.Add("@type", typeTransactionDebt); 
      cmd.Parameters.Add("@with_person", _debter); 
      cmd.Parameters.Add("@note", _note); 
      cmd.Parameters.Add("@amount", _amount); 
      cmd.Parameters.Add("@displayed_date", _ngay_tra); 
      cmd.Parameters.Add("@remind_date", _ngay_tra); 
      cmd.Parameters.Add("@user_id", _user_id); 
      cmd.ExecuteNonQuery(); 
      //MessageBox.Show("debt"); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.ToString()); 
     } 
     finally 
     { 
      closeConnection(); 
     } 
    } 

我被insertNewDebt和insertNewIncome menthod插入一些记录表,没关系,没有任何错误。

这是menthod是让所有的记录表:

public List<ItemTransaction> loadTransactionAll(DateTime _start_date, DateTime _end_date, int _user_id) 
    { 
     List<ItemTransaction> li = new List<ItemTransaction>(); 
      openConnection(); 

      SqliteCommand cmd = conn.CreateCommand(); 
      cmd.CommandText = "SELECT transactions.id AS master_id,transactions.name,transactions.amount,transactions.type,transactions.created_date, transactions.displayed_date,transactions.cat_id, transactions.with_person,transactions.remind_date,remind_num, transactions.note,transactions.status,transactions.user_id,categories.id,categories.name,categories.icon,categories.type,IFNULL(sub_amount,0) AS sub_amount " 
        + "FROM transactions " 
        + "LEFT JOIN categories ON transactions.cat_id = categories.id " 
        + "LEFT JOIN (" 
        + "SELECT sub_trans_id, IFNULL(SUM(sub_amount),0) AS sub_amount " 
        + "FROM (SELECT trans_id AS sub_trans_id,SUM(amount) AS sub_amount " 
        + "FROM sub_transactions " 
        + "WHERE type = @type_income " 
        + "GROUP BY sub_trans_id " 
        + "UNION ALL SELECT trans_id AS sub_trans_id,SUM(amount) * -1 AS sub_amount " 
        + "FROM sub_transactions " 
        + "WHERE type = @type_expense GROUP BY sub_trans_id) GROUP BY sub_trans_id) ON sub_trans_id = master_id " 
        + "WHERE (displayed_date BETWEEN @start_date AND @end_date) " 
        + "AND transactions.user_id = @user_id " 
        + "ORDER BY displayed_date DESC, transactions.type ASC, status ASC"; 
      cmd.Parameters.Add("@type_income", DatabaseProccess.typeTransactionIncome); 
      cmd.Parameters.Add("@type_expense", DatabaseProccess.typeTransactionExpense); 
      cmd.Parameters.Add("@start_date", _start_date); 
      cmd.Parameters.Add("@end_date", _end_date); 
      cmd.Parameters.Add("@user_id", _user_id); 

      SqliteDataReader reader = cmd.ExecuteReader(); 
      while (reader.Read()) 
      { 
       ItemTransaction item = new ItemTransaction(); 
       item.master_id = reader.GetInt32(0); 
       item.name = reader.GetString(1); 
       item.amount = reader.GetDouble(2); 
       item.type = reader.GetInt32(3); 
       item.created_date = reader.GetDateTime(4); 
       item.displayed_date = reader.GetDateTime(5); 
       item.cat_id = reader.GetInt32(6); 
       item.with_person = reader.GetString(7); 
       item.remind_date = reader.GetDateTime(8); 
       item.remind_num = reader.GetInt32(9); 
       item.note = reader[10].ToString(); 
       item.status = reader.GetBoolean(11); 
       item.user_id = reader.GetInt32(12); 
       item.setCategory(reader.GetInt32(13), reader.GetString(14), reader.GetString(15), reader.GetInt32(16)); 
       item.sub_amount = reader.GetDouble(17); 

       li.Add(item); 
      } 

     return li; 
    } 

但是,当我执行INSERT:

insertNewIncome("", "note", "1000", DateTime.Today, 1, 1); 

与 '名称' 字段值= “”

我不能阅读'名称'字段。调试说:

view error screen shot here, please

这是ItemTransaction类:

view ItemTransaction class here, please

我不明白这个错误。

请,谢谢

回答

0

你遇到这个错误name为NULL。

下面的if语句可以解决这个问题:

if (reader(1) != null){ 
    item.name = reader.GetString(1); 
} 

然而,如果你传递的价值"",那么这不应该被存储为NULL。

检查您的数据库,看看它的价值。也许你有一个触发器,将""更新为null