当前位置:网站首页>Comparison of two ways for C to access SQL Server database (SqlDataReader vs SqlDataAdapter)
Comparison of two ways for C to access SQL Server database (SqlDataReader vs SqlDataAdapter)
2022-06-30 08:37:00 【The bright moon is like me】
1. Use SqlDataReader Reading data
public bool CheckResultFromDatabase(string guid)
{
string connStr = "Data Source = localhost; Initial Catalog = TestDB; User ID = sa; Password = Abc12345";
// Create connection
SqlConnection Myconnect = new SqlConnection(connStr);
Myconnect.Open();
// Build up query
StringBuilder sb = new StringBuilder();
sb.Append($"select id, name from dbo.table_a where id = '{
guid}';");
sb.Append($"select student_id, student_name from dbo.student where student_name = 'zhangsan';");
string sql = sb.ToString();
SqlCommand sqlQuery = new SqlCommand(sql, Myconnect);
int rowCount = 0;
bool [] checkresult = {
false, false };
int expectedResultNum = 2;
int currentResult = 0;
using (SqlDataReader reader = sqlQuery.ExecuteReader())
{
do
{
rowCount = 0;
while (reader.Read())
{
rowCount++;
}
if (rowCount == 1)
{
checkresult[currentResult++] = true;
if (currentResult == expectedResultNum)
break;
}
} while (reader.NextResult()); // go to result of next sql query
reader.Close();
}
Myconnect.Close(); // close databse
return checkresult[0] && checkresult[1];
}
2. Use SqlDataReader Reading data
public bool CheckResultFromDatabaseAdapter(string guid)
{
string connStr = "Data Source = localhost; Initial Catalog = TestDB; User ID = sa; Password = Abc12345";
bool resultConfig = false;
bool resultSummary = false;
using (SqlConnection conn = new SqlConnection(connStr))
{
string strSql = $"select id, name from dbo.table_a where id = '{
guid}';";
using (SqlDataAdapter adapter = new SqlDataAdapter(strSql, conn))
{
DataTable dt = new DataTable();
adapter.Fill(dt);
if (dt.Rows.Count == 1)
{
resultConfig = true;
}
//dt.Rows[0][1] //1st record 1st row
//foreach (DataRow dataRow in dt.Rows)
//{
// Console.WriteLine(dataRow["SimulationId"]);
//}
}
strSql = $"select student_id, student_name from dbo.student where student_name = 'zhangsan';";
using (SqlDataAdapter adapter = new SqlDataAdapter(strSql, conn))
{
DataTable dt = new DataTable();
adapter.Fill(dt);
if (dt.Rows.Count == 1)
{
resultSummary = true;
}
}
}
return resultConfig & resultSummary;
}
3. Comparison of the two methods
The two pieces of code above , Function the same , namely : Read data that meets a certain condition from two tables , If the respective record numbers are 1, Then return to true, Otherwise return to false.
SqlDataReader Is characterized by :
- Multiple queries can be done at one time , It must also be done at one time . Unified return and pass NextResult() To switch to the next result set . The most obvious benefit of this is to reduce client and db server The number of interactions , In some cases, it can greatly improve the efficiency of the program .
- No, RowCount Properties or methods , If you want to know the number of records in a result set , You can only count by yourself after traversing . All in all , Not very friendly .
Finish the former ,SqlDataAdapter Of course, the characteristics of :
- Interface is rich , For programmers , It's a lot easier .
- You can query multiple times , It helps to improve the readability of the program .
边栏推荐
- 127.0.0.1、0.0.0.0和localhost
- 【NVMe2.0b 14-3】Doorbell Buffer Config command、Device Self-test command
- Graffiti Wi Fi & ble SoC development slide strip
- Unit Test
- Rendering engine development
- Enhance the add / delete operation of for loop & iterator delete collection elements
- Using typera+picgo to realize automatic uploading of markdown document pictures
- Redis design and Implementation (VII) | publish & subscribe
- Gilbert Strang's course notes on linear algebra - Lesson 4
- icon资源
猜你喜欢
随机推荐
证券开户的优惠怎样才能得到?在线开户安全?
Deploy the cow like customer network project on the ECS
Detectron2 source code reading 2--- using the configurable decorator to build the dataloader
Redis design and Implementation (III) | interaction between server and client (event IO model)
Rendering engine development
Redis design and Implementation (V) | sentinel sentry
[nvme2.0b 14 - 5] commande de téléchargement / commande du logiciel
Redis设计与实现(六)| 集群(分片)
[untitled]
layer.open 当传值为数组或值太长时处理方法
Self made GIF dynamic graph -gifcam
TiDB v6.0.0 (DMR) :缓存表初试丨TiDB Book Rush
酒精测试仪方案:酒精测试仪是根据什么原理测酒精溶度?
Redis设计与实现(四)| 主从复制
Be careful of this hole in transmittable thread local
从0开始构建一个瀚高数据库Docker镜像
Sword finger offer II 076 The kth largest number in the array (use heap to solve TOPK problem)
电流探头电路分析
[untitled]
2021-05-17






![[nvme2.0b 14-7] set features (Part 1)](/img/0d/c26ae2475ae69291d83b4096ea942b.png)


