We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
前段时间在做一个C++的mysql的api封装,对于接口使用上来说,一直没有找到一种比较合适的方案。后来看了下C#的sql访问接口,发现还不错,遂实现了一个简易版的sql接口集合。
C#是采用了面向对象的方式来实现sql访问接口的,使用起来相当的方便,先说说几个重要的对象:
我们直接来看看C#的使用实例,连接数据库,并执行查询操作
private static void ReadOrderData(string connectionString) { SqlConnection connection = new SqlConnection("server= .; uid=hxq; pwd=123456; database=testlazy"); string queryString = "SELECT OrderID, CustomerID FROM dbo.Orders;"; using (SqlConnection connection = new SqlConnection( connectionString)) { SqlCommand command = new SqlCommand( queryString, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); try { while (reader.Read()) { Console.WriteLine(String.Format("{0}, {1}", reader[0], reader[1])); } } finally { // Always call Close when done reading. reader.Close(); } } }
我们的简易版sql访问接口与C#版有很多的区别:
初始化数据库:
DB db; db.init("localhost", "hxq", "123456", "testlazy");
查询使用实例:
void testSelect() { Connection* con = db.getConnection(); SqlCommand* cmd = new SqlCommand("SELECT * FROM USER", con); DataReader reader = cmd->executeReader(); stUser user; //此处stUser是一个结构体 while (reader.read()) { reader.get("ID", user.id); reader.get("AGE", user.age); reader.get("NAME", user.name); reader.get("CITY", user.city); user.print(); } cout << "affected rows: " << cmd->getAffectedRows() << endl; delete cmd; }
插入数据实例:
void testInsert() { stringstream ss; ss << "INSERT INTO USER(AGE,NAME,CITY) VALUES ("; ss << "'24'" <<","<< "'lazy'"<< "," << "'fuling'"; ss << ")"; Connection* con = db.getConnection(); SqlCommand* cmd = new SqlCommand(ss.str(), con); cmd->executeNoQuery(); cout << "affected rows: " << cmd->getAffectedRows() << endl; delete cmd; }
更新数据实例:
void testUpdate() { stringstream ss; ss << "UPDATE USER SET NAME='lazyqiang' WHERE ID=1;"; Connection* con = db.getConnection(); SqlCommand* cmd = new SqlCommand(ss.str(), con); cmd->executeNoQuery(); cout << "affected rows: " << cmd->getAffectedRows() << endl; delete cmd; }
总的来说,这版使用接口还算清晰,具体使用中,sql语句拼接比较繁琐,后续考虑添加一个sql拼接器,也为防止sql注入作一定保证。
代码放在这里。
The text was updated successfully, but these errors were encountered:
No branches or pull requests
前段时间在做一个C++的mysql的api封装,对于接口使用上来说,一直没有找到一种比较合适的方案。后来看了下C#的sql访问接口,发现还不错,遂实现了一个简易版的sql接口集合。
先说说C#的sql访问接口
C#是采用了面向对象的方式来实现sql访问接口的,使用起来相当的方便,先说说几个重要的对象:
我们直接来看看C#的使用实例,连接数据库,并执行查询操作
下面开始谈谈C++简易版实现
我们的简易版sql访问接口与C#版有很多的区别:
初始化数据库:
查询使用实例:
插入数据实例:
更新数据实例:
总的来说,这版使用接口还算清晰,具体使用中,sql语句拼接比较繁琐,后续考虑添加一个sql拼接器,也为防止sql注入作一定保证。
代码放在这里。
The text was updated successfully, but these errors were encountered: