不用存储过程而直接用C#读取数据库的方式:

//C#访问SQL Server数据库,SqlConnection用于建立与数据库的连接
            //SqlCommand用于创建SQL命令
            //SqlDataReader对象是执行SqlCommand对象返回结果集的查询语句后的结果           
            //用于连接的字符串
            string connectString = "data Source=GUCAS-A4ACB77A6\\SQLEXPRESS;Initial Catalog=AcademicSearchInfo;Integrated Security=True";
            SqlConnection connection = new SqlConnection(connectString);
            //select语句
            string sqlConmmandString = "Select * From [User_Category] where UserID = '" + currentUserID + "';";
            SqlCommand sqlCommand = new SqlCommand(sqlConmmandString, connection);
            connection.Open();
            //执行
            SqlDataReader dataReader = sqlCommand.ExecuteReader();
            int fieldCount = dataReader.FieldCount;
            object[] values = new object[fieldCount];
            while (dataReader.Read())
            {
             
                //每一条的数据所有属性都存在一个value数组
                dataReader.GetValues(values);
                string categoryName = values[1].ToString();
                string score = values[2].ToString();
                //Console.WriteLine(categoryName);
                //Console.WriteLine(score);
                //for (int i = 0; i < values.Length; i++)
                //{
                //    Console.WriteLine(values[i].ToString());
                //}
            }
            Console.ReadLine();
            dataReader.Close();
            connection.Close();

你可能感兴趣的:(数据库,职场,休闲)