00001 using System; 00002 using System.Collections; 00003 using System.ComponentModel; 00004 using System.Data; 00005 using System.Data.Common; 00006 00007 namespace SQLiteCSLib 00008 { 00012 public class SQLiteDataAdapter : Component, IDbDataAdapter 00013 { 00017 public SQLiteDataAdapter() 00018 { 00019 } 00020 00021 #region IDbDataAdapter 00022 00026 protected IDbCommand m_updatecmd = null; 00027 00031 public IDbCommand UpdateCommand 00032 { 00033 get 00034 { 00035 return m_updatecmd; 00036 } 00037 set 00038 { 00039 m_updatecmd = value; 00040 } 00041 } 00042 00046 protected IDbCommand m_selectcmd = null; 00047 00051 public IDbCommand SelectCommand 00052 { 00053 get 00054 { 00055 return m_selectcmd; 00056 } 00057 set 00058 { 00059 m_selectcmd = value; 00060 } 00061 } 00062 00066 protected IDbCommand m_deletecmd = null; 00067 00072 public IDbCommand DeleteCommand 00073 { 00074 get 00075 { 00076 return m_deletecmd; 00077 } 00078 set 00079 { 00080 m_deletecmd = value; 00081 } 00082 } 00083 00087 protected IDbCommand m_insertcmd = null; 00088 00092 public IDbCommand InsertCommand 00093 { 00094 get 00095 { 00096 return m_insertcmd; 00097 } 00098 set 00099 { 00100 m_insertcmd = value; 00101 } 00102 } 00103 00104 #endregion 00105 00106 #region IDataAdapter 00107 00113 public int Fill(DataSet dataSet) 00114 { 00115 int iRet = 0; 00116 00117 using( IDataReader reader = SelectCommand.ExecuteReader() ) 00118 { 00119 DataTable dt = reader.GetSchemaTable(); 00120 00121 if( dataSet.Tables.Count > 0 ) 00122 dt.TableName = "Table"+dataSet.Tables.Count.ToString(); 00123 else 00124 dt.TableName = "Table"; 00125 00126 iRet = dt.Rows.Count; 00127 00128 dataSet.Tables.Add( dt ); 00129 } 00130 00131 return iRet; 00132 } 00133 00137 public ITableMappingCollection TableMappings 00138 { 00139 get 00140 { 00141 #if !MOBILEPC 00142 return new DataTableMappingCollection(); 00143 #else 00144 return null; 00145 #endif 00146 } 00147 } 00148 00152 protected MissingSchemaAction m_msa = MissingSchemaAction.Add; 00153 00157 public MissingSchemaAction MissingSchemaAction 00158 { 00159 get 00160 { 00161 return m_msa; 00162 } 00163 set 00164 { 00165 m_msa = value; 00166 } 00167 } 00168 00172 protected MissingMappingAction m_mma = MissingMappingAction.Passthrough; 00173 00177 public MissingMappingAction MissingMappingAction 00178 { 00179 get 00180 { 00181 return m_mma; 00182 } 00183 set 00184 { 00185 m_mma = value; 00186 } 00187 } 00188 00193 public IDataParameter[] GetFillParameters() 00194 { 00195 ArrayList list = m_selectcmd.Parameters as ArrayList; 00196 return list.ToArray( typeof( IDataParameter ) ) as IDataParameter[]; 00197 } 00198 00205 public DataTable[] FillSchema(DataSet dataSet, System.Data.SchemaType schemaType) 00206 { 00207 using( SQLiteDataReader reader = SelectCommand.ExecuteReader() as SQLiteDataReader ) 00208 { 00209 DataTable dt = reader.GetSchemaTable(); 00210 00211 dataSet.Tables.Add( dt ); 00212 00213 return new DataTable[]{dt}; 00214 } 00215 } 00216 00222 public int Update(DataSet dataSet) 00223 { 00224 foreach( DataTable dt in dataSet.Tables ) 00225 { 00226 foreach( DataRow dr in dt.Rows ) 00227 { 00228 switch( dr.RowState ) 00229 { 00230 case DataRowState.Added: 00231 foreach( DataColumn dc in dt.Columns ) 00232 { 00233 InsertCommand.Parameters.Add( dr.ItemArray[ dc.Ordinal ] ); 00234 } 00235 00236 return InsertCommand.ExecuteNonQuery(); 00237 00238 case DataRowState.Deleted: 00239 foreach( DataColumn dc in dt.Columns ) 00240 { 00241 DeleteCommand.Parameters.Add( dr.ItemArray[ dc.Ordinal ] ); 00242 } 00243 00244 return InsertCommand.ExecuteNonQuery(); 00245 00246 00247 case DataRowState.Modified: 00248 foreach( DataColumn dc in dt.Columns ) 00249 { 00250 DeleteCommand.Parameters.Add( dr.ItemArray[ dc.Ordinal ] ); 00251 } 00252 00253 return UpdateCommand.ExecuteNonQuery(); 00254 00255 } 00256 } 00257 } 00258 00259 return 0; 00260 } 00261 00262 #endregion 00263 } 00264 }