param = cmd.Parameters.Add("@Fname",SqlDbType.VarChar,8); param.Direction = ParameterDirection.Output; param = cmd.Parameters.Add("@Fphone",SqlDbType.VarChar,8); param.Direction = ParameterDirection.Output; conn.Open(); cmd.ExecuteNonQuery(); string Fname = cmd.Parameters["@Fname"].Value.ToString(); string Fphone = cmd.Parameters["@Fphone"].Value.ToString(); Console.WriteLine(Fname + " " + Fphone); conn.Close(); showSplit(); } //************************************** //*演示读取多个无关记录集 //************************************** public void multiResult() { SqlCommand cmd; cmd = conn.CreateCommand(); string sqla = "select Fname from friend"; string sqlb = "select Fphone from friend"; cmd.CommandText = sqla + ";" + sqlb; conn.Open(); SqlDataReader reader= cmd.ExecuteReader(); int i = 1; do { Console.WriteLine("第" + i.ToString() + "个记录集内容如下:\n"); while(reader.Read()) { Console.WriteLine(reader[0].ToString() + "\t"); } i++; }while(reader.NextResult()); //NextResult()移动到下一个记录集 reader.Close(); conn.Close(); showSplit(); } //************************************** //*使用DataReader获得数据库模式信息 //************************************** public void getSchema() { SqlCommand cmd; cmd = conn.CreateCommand(); string sql = "select Fid,Fname,Fphone from friend"; cmd.CommandText = sql; conn.Open(); SqlDataReader reader = cmd.ExecuteReader(); DataTable SchemaTable = reader.GetSchemaTable(); DataRowCollection SchemaColumns = SchemaTable.Rows; DataColumnCollection SchemaProps = SchemaTable.Columns; foreach(DataRow SchemaColumn in SchemaColumns) { foreach(DataColumn SchemaColumnProp in SchemaProps) { Console.WriteLine(SchemaColumnProp.ColumnName + "=" + SchemaColumn[SchemaColumnProp.ColumnName].ToString()); } } reader.Close(); conn.Close(); showSplit(); } //************************************** //*从数据库读取二进制数据的代码段 //*该代码段只是读取二进制的片断,不是 //*整个程序,所以不能执行,你可以把它 //*集成到你的WinForm项目里面。 //************************************** public void getBinary() { /* System.IO.MemoryStream stream = new System.IO.MemoryStream(); System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream); int BufferSize = 1024; byte[] Buffer = new Byte[BufferSize]; long Offset = 0; long BytesRead = 0; do { BytesRead = reader.GetBytes(2,Offset,Buffer,0,BufferSize); writer.Writer(Buffer,0,(int)BytesRead); writer.Flush(); Offset += BytesRead; } while(BytesRead == BufferSize); */ } //添加输出分隔 private void showSplit() |