一、sqlDataReader SqlConnection conn = new SqlConnection("server=10.126.64.11;user=it_oper;pw
SqlConnection conn = new SqlConnection("server=10.126.64.11;user=it_oper;pwd=IT@SME2018;database=MESOtheata;");
conn.Open();
SqlCommand cmd = new SqlCommand("select * from [A_Emp_Dept_20190522]", conn);// 或者
//或者 SqlCommand cmd=conn.CreateCommand();cmd.CommandText="";
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);//读取完毕自动关闭连接
for (int i = 0; i < reader.FieldCount - 1; i++)
{
Console.WriteLine(reader.GetName(i));//获取字段名
while (reader.Read())//或者使用reader.HasRows属性判断时候有记录
{
Console.WriteLine(reader.GetDateTime(0).ToString());
//或者 Console.WriteLine(Convert.ToDateTime(reader[0]).ToString());
object[] values = new object[reader.FieldCount - 1];
reader.GetValues(values);//读取一行所有字段的内容到Values字段中
if (reader.IsDBNull(0)
{
//判断某字段是否为null值。
}
}
}
reader.Close();
//系统未关闭时,本Command更改CommandText后或新建Command还有继续使用此连接。
//conn.Close();
1、使用SQLClient:
SqlCommand cmd = new SqlCommand("select * from [A_Emp_Dept_20190522] where empname=@empname", conn);
SqlParameter para = new SqlParameter("@empname", SqlDbType.NVarChar, 10);
para.Value = "曾亮";
cmd.Parameters.Add(para);
//或者直接 cmd.Parameters.AddWithValue("@empname","曾亮");
2、使用OleDb:
OleDbCommand cmd = new OleDbCommand("select * from [A_Emp_Dept_20190522] where empname like ? and line like ?", conn);
cmd.Parameters.AddWithValue("@empname", "曾亮");
cmd.Parameters.AddWithValue("@line", "ODMZ%");//参加的参数的顺序应与?号的顺序相同,参数名可随意执行,甚至可以是空串。
SqlCommand cmd = new SqlCommand("update [A_Emp_Dept_20190522] set dept='aa' where dept='' ", conn);
int RecordAffected = cmd.ExecuteNonQuery();
if (RecordAffected == 0)
{
Console.Write("操作失败");
}
SqlCommand cmd = new SqlCommand("select count(*) from [A_Emp_Dept_20190522] ", conn);
if (Convert.ToInt32(cmd.ExecuteScalar()) == 0)//无记录,返回null
{
Console.Write("无记录");
}
SqlTransaction trans = null;
try
{
trans = conn.BeginTransaction();//默认事务隔离级别为ISolationLevel.Serilizable,oracle为ISolationLevel.ReadCommited
SqlCommand cmd = new SqlCommand("Insert int **", conn);
cmd.Transaction = trans;//为命令指定一个事务
cmd.ExecuteNonQuery();
//可以有多个SqlCommand一并执行,一起提交或回滚
trans.Commit();
}
catch
{
trans.Rollback();
}
Transaction事务隔离级别:
SqlCommand cmd = new SqlCommand("GetCustomer", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter para1 = new SqlParameter("@CustomerID", SqlDbType.NChar, 10);
para1.Value = "UINET";
cmd.Parameters.Add(para1);
SqlParameter para2 = new SqlParameter("@CompanyName", SqlDbType.NVarChar, 30);
para2.Direction = ParameterDirection.Output;
cmd.Parameters.Add(para2);
cmd.ExecuteNonQuery();
Console.WriteLine(cmd.Parameters["@CompanyName"].Value);
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly);//仅仅读取模式信息
DataTable table = reader.GetSchemaTable();
foreach (DataRow row in table.Rows)
{
foreach (DataColumn col in table.Columns)
{
Console.WriteLine(col.ColumnName + row[col.ColumnName]);
}
}
SqlCommand cmd = new SqlCommand("select * from CatoGories;select * from Customers", conn);
SqlDataReader reader = cmd.ExecuteReader();//仅仅读取模式信息
DataTable table = reader.GetSchemaTable();
do//首先自动定位在第一个结果集上
{
while (reader.Read())
{
Console.Write(reader[0]);
}
} while (reader.NextResult());
从数据源中读取行的只进流,包含SqlDataReader ,OleDbDataReader ,OdbcDataReader ,实现了IDataReader IDataRecord 接口
到此这篇关于C#SQLCommand命令与DbTransaction事务处理的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持编程网。
--结束END--
本文标题: C#中的SQLCommand命令与DbTransaction事务处理
本文链接: https://lsjlt.com/news/148548.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0