mssql数据集操作方法
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string constring = "data source=127.0.0.1;Database=;user id=;password=";
SqlConnection myconn = new SqlConnection(constring);
string sql = "";
//SqlCommand mycmd = new SqlCommand(sql);
// mycmd.Connection = myconn;
SqlCommand mycmd = new SqlCommand(sql, myconn);
myconn.Open();
int rows = mycmd.ExecuteNonQuery();//返回受影响的行数,多用于 insert,update,delete,create
myconn.Close();
string sql2 = "";
SqlCommand mycmd2 = new SqlCommand(sql2, myconn);
myconn.Open();
int count = (int)mycmd.ExecuteScalar();//返回单个值,返回记录集中第一行第一列,忽略额外的行和列
myconn.Close();
string sql3 = "";
SqlCommand mycmd3 = new SqlCommand(sql3, myconn);
myconn.Open();
SqlDataReader dr = new SqlDataReader();//返回一行或多行,多用于select
while (dr.Read())
{
//遍历结果集
Response.Write(dr.GetInt32(0) + " " + dr.GetString(1).ToString());
//遍历结果集,使用序数索引器
Response.Write(dr[0].ToString() + " " + dr[1].ToString());
//遍历结果集,使用列名索引器
Response.Write(dr["id"].ToString() + " " + dr["name"].ToString());
//FieldCount(获取当前行中的列数【int】),HasRows(获取当前记录集中的是否包含一行或多行【bool】)
}
dr.Close();
myconn.Close();
string sql4 = "sql1;sql2";
SqlCommand mycmd4 = new SqlCommand(sql4, myconn);
myconn.Open();
SqlDataReader drm = new SqlDataReader();//返回一行或多行,多用于select
do
{
while (drm.Read())
{
//遍历结果集
Response.Write(drm.GetInt32(0) + " " + drm.GetString(1).ToString());
//遍历结果集,使用序数索引器
Response.Write(drm[0].ToString() + " " + drm[1].ToString());
//遍历结果集,使用列名索引器
Response.Write(drm["id"].ToString() + " " + drm["name"].ToString());
}
} while (drm.NextResult());
drm.Close();
myconn.Close();
using (SqlConnection conn = new SqlConnection(constring))
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "sql6";
conn.Open();
using (SqlDataReader drx = cmd.ExecuteReader())//返回一行或多行,多用于select
{
while (drx.Read())
{
//遍历结果集
Response.Write(drx.GetInt32(0) + " " + drx.GetString(1).ToString());
//遍历结果集,使用序数索引器
Response.Write(drx[0].ToString() + " " + drx[1].ToString());
//遍历结果集,使用列名索引器
Response.Write(drx["id"].ToString() + " " + drx["name"].ToString());
}
}
}
}
public void getDataAdapter(){
string constring = "data source=127.0.0.1;Database=;user id=;password=";
SqlConnection myconn = new SqlConnection(constring);
SqlDataAdapter adapter = new SqlDataAdapter("select * from userinfo ", myconn);
//SqlCommand cmd = new SqlCommand("select * from userinfo where id>@id ",myconn);
//cmd.Parameters.AddWithValue("@id",str);
//SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable data = new DataTable();
adapter.Fill(data);
for (int i = 0; i < data.Rows.Count; i++)
{
Response.Write(data.Rows[i]["id"]+" "+data.Rows[i]["name"]);
}
// DataRow dr = data.Rows[n];
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗