标签:net sys ase 执行 static lda 取数 nbsp 模式
string connectionString="data source=.;initial catalog=Northwind;persist security info=True;user id=sa;password=abcd1234";
using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); }
2.commnd
(1)ExecuteNonQuery(),执行非 SELECT 语句,如增、删、改。返回受影响的行数,int型。
string connectionString="data source=.;initial catalog=Northwind;persist security info=True;user id=sa;password=abcd1234"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); //在执行ExecuteNonQuery之前打开就行 string sql="update Employees set name=‘zhangsan‘"; SqlCommand cmd = new SqlCommand(sql, connection);
int result=Convert.ToInt32(cmd.ExecuteNonQuery());
connection.Close(); //在执行ExecuteNonQuery之后关闭就行
}
(2)ExecuteScalar() ,执行 SELECT 查询,返回第一行第一列的值,类型为 Object,常用语执行聚合函数
string connectionString="data source=.;initial catalog=Northwind;persist security info=True;user id=sa;password=abcd1234"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); //在执行ExecuteNonQuery之前打开就行 string sql="SELECT * FROM Employees"; SqlCommand cmd = new SqlCommand(sql, connection); int result=Convert.ToInt32(cmd.Executescalar()); //返回的是object类型,需要转成int型 connection.Close(); //在执行ExecuteNonQuery之后关闭就行 }
(3)ExecuteReader(),执行 SELECT 查询,返回一个封装了只读,只进游标的 DataReader 对象
3.DateSet
private static DataSet getDataset() { string connectionString = "Data Source=192.168.0.245;User ID=sa;Password=123456;Persist Security Info=True;Initial Catalog=MES928"; using (SqlConnection connection = new SqlConnection(connectionString)) { string sql = "SELECT * FROM Employees"; SqlDataAdapter adapter = new SqlDataAdapter(); DataSet ds = new DataSet(); adapter.SelectCommand = new SqlCommand(sql, connection); adapter.Fill(ds); return ds; } }
标签:net sys ase 执行 static lda 取数 nbsp 模式
原文地址:http://www.cnblogs.com/haigui-zx/p/6000604.html