Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common C # codes in database?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces what the database commonly used C # code has, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Create a database

/ / create the database File.Delete ("Test.sdf"); SqlCeEngine engine = new SqlCeEngine ("Data Source='Test.sdf';LCID=1033;Password=\" slottery 2); Encrypt=TRUE; "); engine.CreateDatabase ()

Verify and repair the database

/ / verify and repair the database SqlCeEngine engine = new SqlCeEngine ("Data Source=AdventureWorks.sdf"); if (false = = engine.Verify ()) {MessageBox.Show ("Database is corrupted."); engine.Repair (null, RepairOption.RecoverCorruptedRows);}

Compress database

/ / compress the database / / reclaim the wasted space in the SQL Server Mobile database by creating a new database file from the existing file. / / this method can also be used to change the sort order, encryption, or password settings of the database. / / the connection string specifies a connection to the target database to be created by this method. An exception is thrown if the specified database already exists or another file with the same name already exists. / / if you pass an empty string for the connection string, the new database file will overwrite the old database file, / / but the name remains the same. SqlCeEngine engine = new SqlCeEngine ("Data Source=AdventureWorks.sdf"); / / engine.Compact (null); engine.Compact ("Data Source=; passwordholders)

Shrink the database

/ / shrink the database / / reclaim wasted space in the SQL Server Mobile database by moving empty pages to the end of the file and truncating the file. / / unlike the Compact method, the Shrink method does not create a tempdb file, / / but moves all empty and unallocated pages to the end of the file and then truncates, thereby reducing the total size of the database. SqlCeEngine engine = new SqlCeEngine ("Data Source=AdventureWorks.sdf"); engine.Shrink ()

Merge replication

/ / merge replication / / instantiate and configure SqlCeReplication objects SqlCeReplication repl = new SqlCeReplication (); repl.InternetUrl = "http://www.adventure-works.com/sqlmobile/sqlcesa30.dll"; repl.InternetLogin =" MyInternetLogin "; repl.InternetPassword ="; repl.Publisher =" MyPublisher "; repl.PublisherDatabase =" MyPublisherDatabase "; repl.PublisherLogin =" MyPublisherLogin "; repl.PublisherPassword ="; repl.Publication =" MyPublication "; repl.Subscriber =" MySubscriber " Repl.SubscriberConnectionString = "Data Source=MyDatabase.sdf"; / / create a subscription repl.AddSubscription (AddOption.CreateDatabase) of a local SQL Server Mobile database; / / synchronize repl.Synchronize () with the SQL Server database; / / clean up the repl object repl.Dispose ()

Remote data access (RDA)

/ / remote data access / / instantiate and configure SqlCeRemoteDataAccess objects SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess (); rda.InternetUrl = "http://www.adventure-works.com/sqlmobile/sqlcesa30.dll"; rda.InternetLogin =" MyInternetLogin "; rda.InternetPassword ="; rda.LocalConnectionString =" Data Source=MyDatabase.sdf "; / / download data rda.Pull (" Employees "," SELECT * FROM DimEmployee "," Provider=sqloledb;server=MySqlServer;database=AdventureWorks;uid=sa;pwd= ") from SQL Server ", RdaTrackOption.TrackingOnWithIndexes," ErrorTable "); / / modify local data / upload modified data to SQL Server rda.Push (" DimEmployee "," Provider=sqloledb;server=MySqlServer;database=AdventureWorks;uid=sa;pwd=; "); / / submit SQL statements to execute rda.SubmitSql (" CREATE TABLE MyRemoteTable (colA int) "," Provider=sqloledb;server=MySqlServer;database=AdventureWorks;uid=sa;pwd=; ") on SQL Server

Use SqlCeResultSet

/ / use SqlCeResultSet / / to create a SQL Server Mobile database connection SqlCeConnection conn = new SqlCeConnection ("Data Source=Northwind.sdf"); / / create and configure SqlCeCommand objects SqlCeCommand cmd = conn.CreateCommand (); cmd.CommandText = "SELECT * FROM Orders"; / / create SqlCeResultSet objects and configure them to be scrollable, updatable, and detect data source changes ResultSetOptions options = ResultSetOptions.Scrollable | ResultSetOptions.Sensitive | ResultSetOptions.Updatable; SqlCeResultSet resultSet = cmd.ExecuteResultSet (options) / / create a ResultSetView object that is configured to display only the column ResultSetView resultSetresultSetView = resultSet.ResultSetView; int [] ordinals = new int [] {1mine3pm 5re8}; resultSetView.Ordinals = ordinals; / / bind ResultSetView to the DataGrid control this.dataGrid.DataSource = resultSetView

Dealing with SqlCeException

/ / handle SqlCeException public static void ShowErrors (SqlCeException e) {SqlCeErrorCollection eerrorCollection = e.Errors; StringBuilder bld = new StringBuilder (); Exception inner = e.InnerException; foreach (SqlCeError err in errs) {/ / HRESULT values that identify the type of errors that are not inherent in SQL Server CE ("\ r\ nError Code:") .append (err.HResult.ToString ("X")) / / A text bld.Append ("\ r\ nMessage:") .append (err.Message) that describes the error; / / gets the native error number of SqlCeError bld.Append ("\ r\ nMinor Err.:") .append (err.NativeError); / / the name of the provider that generated the error bld.Append ("\ r\ nSource:") .append (err.Source); / / traverses the first three error parameters. SQL Server CE uses error parameters to provide additional details about the error. Foreach (int numPara in err.NumericErrorParameters) {/ / although errors may have parameters, not all errors that occur return parameters. / / if no parameters are returned when an error occurs, the value of the array is 0. If (numPara! = 0) {bld.Append ("\ r\ nNum. Par.:") .append (numPara);}} / / traversal * * three error parameters. SQL Server CE uses error parameters to provide additional details about the error. Foreach (string errPara in err.ErrorParameters) {/ / although errors may have parameters, not all errors that occur return parameters. / / if no parameters are returned when an error occurs, the value of the array will be an empty string. If (errPara! = String.Empty) {bld.Append ("\ r\ nErr. Par.: ") .append (errPara);} MessageBox.Show (bld.ToString ());} Thank you for reading this article carefully. I hope the article" what are the common C # codes in the database "shared by the editor will be helpful to you? at the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report