In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to operate the database through ServiceStack's OrmLite". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
OrmLite, the operation class library of the database, is integrated in ServiceStack, which simplifies the tedious operation of reading the database when writing the service code. Through ORM, the data in the database can be read and mapped directly to the model in C #. Of course, this kind of library can also be used separately as a class library for C # to operate a database.
Object-relational mapping (Object Relational Mapping, referred to as ORM, or O/RM, or Omax R mapping) is a programming technology used to convert data between different types of systems in object-oriented programming languages. In effect, it actually creates a "virtual object database" that can be used in a programming language. -- from the Internet
The following is an example of how to manipulate a database through ServiceStack's OrmLite.
The referenced class library, which must be referenced in the red box
ServiceStack.OrmLite.SqlServer is based on the database to which you want to connect, on demand, using databases such as mysql,postgresql, you need to refer to the corresponding version of the class library.
Add the connection string to the database in the configuration, which is the same as the connection string normally used to develop the database in C #, for example, the connection to sqlserver:
Data Source=ip address
Initial Catalog= database name
User ID=sa
Password= password
Initialize a connection to a database:
Var _ sqlserverFla =
New OrmLiteConnectionFactory
('connection string', SqlServerDialect.Provider)
Since it is orm, we need to establish a corresponding model according to the database in order to operate in the database. In C #, the fields in model,model should be consistent with the corresponding names in the data table, and the data types should be consistent as far as possible.
Public class model1
{
Public string prop1 {get; set;}
Public string prop2 {get; set;}
Public double prop3 {get; set;}
}
Open the connection to the database, determine whether there are objects, delete the data of objects, you can use this way, you can also write sql statements.
Using (var db = _ sqlserverFla.Open ())
{
If (db.Exists (string.Format ("select * from model1 where currentdate=' {0}'", _ currentdate)
Db.ExecuteSql (string.Format ("delete from model1 where currentdate=' {0}'", _ currentdate))
}
Gets a single bar of data, and the result is a model object.
Var _ aqiDayLst = db.Single ("sql statement")
Get multiple pieces of data, a model object of list
Var lstmodel = db.Select ("sql statement")
Insert data, either a single piece of data or multiple pieces of data:
Var _ isSuc = db.Save ('data')
This is the end of the content of "how to operate the database through ServiceStack's OrmLite". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.