Dev/Mobile

Compact Framework을 이용한 간단한 RDA 예제

newtype 2003. 7. 18. 17:50
요번에 VS.NET 2003 설치하구 잠깐 짬내서 해봤습니다.
MR은 서버 셋팅이 까다로와서 RDA방식으로 했구요.
EVC로 하다가 C#으로 하니 엄청 편하네여..@.@



=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


               private void button1_Click(object sender, System.EventArgs e)
               {

                       string strLocalDB = "\\NorthWind.sdf";
                       string strCon = "Provider=microsoft.sqlserver.oledb.ce.2.0;Data Source=" + strLocalDB;
                       string strOleCon = "provider=sqloledb; data source=192.168.0.2; Initial Catalog=NorthWind; user id=sa; password=sa";
                       string strSQL = "SELECT ProductID, ProductName, UnitPrice FROM Products";

                       // 기존 파일 삭제
                       System.IO.File.Delete( strLocalDB );


                       // Local DB 생성
                       SqlCeEngine engine = new SqlCeEngine( strCon );
                       engine.CreateDatabase();

                       System.Data.SqlServerCe.SqlCeRemoteDataAccess rda;
                       SqlCeConnection con;
                       SqlCeCommand cmd;
                       SqlCeDataReader reader;

                       try
                       {
                               

                               // RDA Pull
                               rda        = new SqlCeRemoteDataAccess( "http://192.168.0.2/mobile/sscesa20.dll",                
                                                                    "", "", strCon );
                               rda.Pull( "Products", strSQL, strOleCon );

                               // Local Open
                               con        = new SqlCeConnection( "Data Source = \\Northwind.sdf;" );
                               cmd = new SqlCeCommand( strSQL, con );
                               cmd.CommandType = CommandType.Text;

                               cmd.Connection.Open();
                               reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                               while( reader.Read() )
                               {
                                       System.Windows.Forms.ListViewItem lvi;
                                       lvi = new ListViewItem( reader.GetValue(0).ToString() );
                                       lvi.SubItems.Add( reader.GetValue(1).ToString() );
                                       lvi.SubItems.Add( reader.GetValue(2).ToString() );

                                       listView1.Items.Add( lvi );
                                       
                               }

                               reader.Close();
                               con.Close();

                       }
                       catch(SqlCeException ex)
                       {
                               SqlCeErrorCollection errorCollection = ex.Errors;

                               StringBuilder bld = new StringBuilder();
                               Exception   inner = ex.InnerException;

                               if (null != inner)
                               {
                                       MessageBox.Show("Inner Exception: " + inner.ToString());
                               }

                               foreach (SqlCeError err in errorCollection)
                               {
                                       bld.Append("\n Error Code: " + err.HResult.ToString("X"));
                                       bld.Append("\n Message   : " + err.Message);
                                       bld.Append("\n Minor Err.: " + err.NativeError);
                                       bld.Append("\n Source    : " + err.Source);
           
                                       foreach (int numPar in err.NumericErrorParameters)
                                       {
                                               if (0 != numPar) bld.Append("\n Num. Par. : " + numPar);
                                       }
           
                                       foreach (string errPar in err.ErrorParameters)
                                       {
                                               if (String.Empty != errPar) bld.Append("\n Err. Par. : " + errPar);
                                       }

                                       MessageBox.Show(bld.ToString());
                                       bld.Remove(0, bld.Length);
                               }
                       }

               }
반응형