using System.Data; using System.Data.SqlClient; SqlConnection con = new SqlConnection(); con.ConnectionString = "server=.;database=stu;uid=sa;pwd=sa"; con.Open(); /* SqlDataAdapter 对象。 用于填充DataSet (数据集)。 SqlDataReader 对象。 从数据库中读取流.. 后面要做增删改查还需要用到 DataSet 对象。 */ //新增、修改、删除 SqlCommand com = new SqlCommand(); com.Connection = con; com.CommandType = CommandType.Text; com.CommandText = "update users set [email protected],[email protected] where [email protected]"; SqlParameter parn = new SqlParameter("@name", name); cmd.Parameters.Add(parn); SqlParameter parp = new SqlParameter("@pwd", pwd); cmd.Parameters.Add(parp); SqlParameter pari = new SqlParameter("@id", id); cmd.Parameters.Add(pari); cmd.ExecuteNonQuery() conn.Close(); cmd.Dispose(); //若是执行存储过程(例) cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@id", SqlDbType.VarChar, 64).Value = orderid; cmd.Parameters.Add("@PrintNo", SqlDbType.Int, 32).Value = mPrintNo; cmd.CommandText = "UpdateSalePrintNo"; cmd.ExecuteNonQuery(); //查询 string sql = "select * from users"; SqlCommand cmd = new SqlCommand(sql, conn); //或 SqlCommand cmd = new SqlCommand() cmd.Connection =con; cmd.CommandText = sql ; SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); conn.Close(); cmd.Dispose(); return dt;
时间: 2024-10-13 00:08:38