当前位置:网站首页>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 .
边栏推荐
- Redis design and Implementation (I) | data structure & object
- Flink SQL 自定义 Connector
- Flink SQL custom connector
- 【NVMe2.0b 14-4】Directive Send/Receive command
- VIM from dislike to dependence (21) -- cross file search
- Mmcv expanding CUDA operator beginner level chapter
- Gilbert Strang's course notes on linear algebra - Lesson 2
- Redis设计与实现(七)| 发布 & 订阅
- vite项目require语法兼容问题解决require is not defined
- Leetcode47. full arrangement II
猜你喜欢

电流探头的干扰源电流谱测试

Redis design and Implementation (III) | interaction between server and client (event IO model)

【NVMe2.0b 14-3】Doorbell Buffer Config command、Device Self-test command

【NVMe2.0b 14-2】Create/Delete Queue

Flink 数据偶尔数据积压导致checkpoint失败

Codeworks 5 questions per day (1700 for each) - the third day

电流探头电路分析

TiDB 6.0:让 TSO 更高效丨TiDB Book Rush

【NVMe2.0b 14-7】Set Features(上篇)

【NVMe2.0b 14】NVMe Admin Command Set
随机推荐
我们如何拿到自己满意的薪资呢?这些套路还是需要掌握的
[untitled]
Leetcode47. full arrangement II
Gilbert Strang's course notes on linear algebra - Lesson 4
维基媒体基金会公布新商业产品“维基媒体企业”首批客户
Camera
Mmcv expanding CUDA operator beginner level chapter
Axure make menu bar effect
Pytorch BERT
Using typera+picgo to realize automatic uploading of markdown document pictures
[untitled]
【NVMe2.0b 14-6】Format NVM、Keep Alive、Lockdown command
酒精测试仪方案:酒精测试仪是根据什么原理测酒精溶度?
Summary of common pytoch APIs
2021-04-29
技术管理进阶——管理者如何进行梯队设计及建设
MIME type Encyclopedia
Is it safe to open an account? How can anyone say that it is not reliable.
【NVMe2.0b 14-3】Doorbell Buffer Config command、Device Self-test command
2021-02-22