[.net中]数据操作应该注意的问题

  

static public DataSet xx()
  
{
    
string  Sql_Insert_Login_Stat = "select * from member where card_no = @card_no and id = @id";

   DataSet ds 
= new DataSet();

   SqlConnection conn 
= new SqlConnection(Config.ConfigSection["webconnstr"]);
   
   
try
   
{
    conn.Open();
    
using(SqlCommand cmd = new SqlCommand(Sql_Insert_Login_Stat)) 
    
{
     cmd.Connection 
= conn; 

     cmd.CommandType 
= CommandType.Text;   
       
     cmd.Parameters.Add(
"@card_no","153007"); 
     cmd.Parameters.Add(
"@id",3);    
     
     
using(SqlDataAdapter da = new SqlDataAdapter()) 
     
{
      da.SelectCommand 
= cmd;
      da.Fill(ds);       
     }
    

    }


   }

   
catch
   
{
   
   }

   
finally
   
{
    conn.Close();
    
   }


   
return ds;


  }




1.使用从一个table中取数据时,要确保,要使用该行数据存在
2.使用强制类型转换时要确保数据不为空.
3.在拼sql语句时要注意防止因为参数为空出现:
select * from member where UserID = and xx = 1
4.要注意防止sql注入引起得问题.
5.判断一个一个字段是否为空用System.DBNull.Value.
6.尽可能的使用存储过程.可以解决问题3,
7.在插入一条记录时,检查,记录是否存在,如果不存在插入,存在则更新.

posted @ 2005-07-15 15:15  烈马狂生  阅读(290)  评论(0编辑  收藏  举报