| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 775 人关注过本帖
标题:如何读取数据库里面的信息!求大神!
只看楼主 加入收藏
yebanbaobei
Rank: 2
等 级:论坛游民
帖 子:20
专家分:14
注 册:2012-12-24
结帖率:66.67%
收藏
已结贴  问题点数:10 回复次数:7 
如何读取数据库里面的信息!求大神!
没接触过数据库!想自己试下如何存取和读取,修改数据库里面的信息!要用什么方法?
搜索更多相关主题的帖子: 如何 信息 数据库 
2013-04-20 15:01
Aviva_Wang
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:苏州
等 级:贵宾
威 望:26
帖 子:701
专家分:2382
注 册:2013-3-12
收藏
得分:3 
用VS读吗?还是直接操作数据库?

asp
2013-04-20 15:19
yebanbaobei
Rank: 2
等 级:论坛游民
帖 子:20
专家分:14
注 册:2012-12-24
收藏
得分:0 
比如1:我存了数据进去。但我想修改存进去的资料,如何修改?有何方法?
2:存了数据进数据库,想通过数据库把数据取出来,然后在窗口里面现实出来,如何操作?

[ 本帖最后由 yebanbaobei 于 2013-4-20 15:29 编辑 ]
2013-04-20 15:27
Aviva_Wang
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:苏州
等 级:贵宾
威 望:26
帖 子:701
专家分:2382
注 册:2013-3-12
收藏
得分:0 
/* 保存到数据库 */
            string  constring = "SERVER = 服务名; DATABASE = 数据库名 ; UID = sa ; Pwd = 密码";//数据库连接对象
            // 1. 创建 connection 对象
            SqlConnection con = new SqlConnection(constring);

            // 2.创建 command 对象
            SqlCommand cmd = new SqlCommand();

            // 3.设置command 属性
            cmd.Connection = con;

            /* 判断是添加还是修改 */
            //  查询  
             ="select * from table";
           // 添加
         
                = string.Format("insert into commodities(CName , SortId , Price , IsDiscount , DiscountPrice) " +
                                               " values('{0}' , {1} , {2} ,{3} , {4})", name, type, money, Discount, price);
            //修改
                = string.Format("update Commodities set CName = '{0}' , SortId = {1} , Price = {2} , " + " IsDiscount = {3} , DiscountPrice = {4} where CId = {5}" ,name , type , money , Discount , price , UserHelp.NowRow.Cells[0].Value);
         
            // 4.打开链接
            con.Open();
            // 5.执行操作(不是查询用这个返回值)
           int result = cmd.ExecuteNonQuery();

            // 6. 关闭连接
            con.Close();

asp
2013-04-20 15:29
yebanbaobei
Rank: 2
等 级:论坛游民
帖 子:20
专家分:14
注 册:2012-12-24
收藏
得分:0 
可否详细一点?或者有什么例子之类的 谢谢
2013-04-20 15:53
hhh19880107
Rank: 1
等 级:新手上路
帖 子:3
专家分:5
注 册:2013-4-20
收藏
得分:3 
sqlconnection ,sqlcommand dataset 等 在Datagridview 中显示
2013-04-20 18:32
ming_cl
Rank: 2
等 级:论坛游民
威 望:1
帖 子:6
专家分:11
注 册:2013-4-21
收藏
得分:3 
学习了
2013-04-22 15:57
lxb932979339
Rank: 5Rank: 5
等 级:职业侠客
威 望:4
帖 子:84
专家分:313
注 册:2013-4-24
收藏
得分:3 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            BookModel model = new BookModel();
            Console.WriteLine("请输入书籍类别号");
            model.FName = Console.ReadLine();
            Console.WriteLine("请输入书籍类别号");
            model.FCategoryID = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入作者名称");
            model.FAuthor = Console.ReadLine();
            Console.WriteLine("请输入出版日期");
            model.FYearPublished = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入是否删除(false/ture)");
            model.IsDel = Convert.ToBoolean(Console.ReadLine());

            int rel = new Program().Add(model);//因为静态方法不能直接调用非静态方法,所以要实例化一个新的对象进行调用
            List<BookModel> list = new List<BookModel>();
            list = (List<BookModel>)(new Program().Table());
            foreach (BookModel m in list)
            {
                Console.WriteLine(m.FAuthor.ToString());
                Console.WriteLine(m.FCategoryID.ToString());
                Console.WriteLine(m.FId.ToString());
                Console.WriteLine(m.FName.ToCharArray());
                Console.WriteLine(m.FYearPublished.ToString());
                Console.WriteLine(m.IsDel.ToString());
            }
            Console.ReadKey();
        }
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
         public int Add(BookModel model)
        {
            string sqlText = "insert into T_Book(FAuthor,FCategoryID,FName,FYearPublished,IsDel)values(@FAuthor,@FCategoryID,@FName,@FYearPublished,@IsDel);select @@identity";
            object rel = SqlHelper.ExecuteScalar(sqlText, new SqlParameter("FAuthor", ModelItemIsNow(model.FAuthor)),
                new SqlParameter("FCategoryID", ModelItemIsNow(model.FCategoryID)),
                new SqlParameter("FName", ModelItemIsNow(model.FName)),
                new SqlParameter("FYearPublished", ModelItemIsNow(model.FYearPublished)),
                new SqlParameter("IsDel", ModelItemIsNow(model.IsDel)));
            return Convert.ToInt32(rel);
        }
         public IEnumerable<BookModel> Table()
         {
             string sqlText = "select * from T_Book";
             DataTable tb = new DataTable();
             tb = SqlHelper.ExecuteDataTable(sqlText);
             List<BookModel> list = new List<BookModel>();
             foreach (DataRow row in tb.Rows)
             {
                 BookModel model = new BookModel();
                 model.FAuthor = row.IsNull("FAuthor") ? null : (string)row["FAuthor"];
                 model.FCategoryID = row.IsNull("FCategoryID") ? null : (int?)row["FCategoryID"];
                 model.FId = (int)row["FId"];//因为FID是主健字段,所以一定是非空字段
                 model.FName = row.IsNull("FName") ? null : (string)row["FName"];
                 model.FYearPublished = row.IsNull("FYearPublished") ? null : (int?)row["FYearPublished"];
                 model.IsDel = row.IsNull("IsDel") ? null : (bool?)row["IsDel"];
                 list.Add(model);
             }
             return list;
         }

        /// <summary>
        /// 判断传递的参数是否为空,若为空,返回DBNULL的值
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public object ModelItemIsNow(object str)
        {
            if (str == null || str.ToString().Trim ().Length <= 0)
            {
                return DBNull.Value;
            }
            else
            {
                return str;
            }
        }
    }
    class SqlHelper
    {
        private static readonly string conStr = "Data Source=.;Initial Catalog=one;Persist Security Info=True;User ID=sa;Password=liuxianbin123";
        private static SqlConnection con;
        /// <summary>
        /// 创建数据库连接
        /// </summary>
        public static SqlConnection Con
        {
            get
            {
                if (con == null || con.State == ConnectionState.Broken)
                {
                    con = new SqlConnection(conStr);
                }
                return con;
            }
        }

        public static DataTable ExecuteDataTable(string sqlText, params SqlParameter[] parameter)
        {
            SqlCommand com = new SqlCommand(sqlText, Con);
             = CommandType.Text;
            if (parameter != null || parameter.Length > 0)
            {
                com.Parameters.AddRange(parameter);
            }
            SqlDataAdapter ad = new SqlDataAdapter(com);
            DataTable tb = new DataTable();
            try
            {
                ad.Fill(tb);
                return tb;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public static object ExecuteScalar(string sqlText, params SqlParameter[] parameter)
        {
            Con.Open();
            SqlCommand com = new SqlCommand(sqlText, Con);
            if (parameter.Length > 0 || parameter != null)
            {
                com.Parameters.AddRange(parameter);
            }
            try
            {
                return com.ExecuteScalar();
            }
            catch (Exception ex)
            {

                throw ex;
            }
            finally
            {
                Con.Close();
            }
        }
        public static int ExecuteNonQury(string sqlText, params SqlParameter[] parameter)
        {
            Con.Open();
            SqlCommand com = new SqlCommand(sqlText, Con);
            if (parameter.Length > 0 || parameter != null)
            {
                com.Parameters.AddRange(parameter);
            }
            try
            {
                return com.ExecuteNonQuery();
            }
            catch (Exception ex)
            {

                throw ex;
            }
            finally
            {
                Con.Close();
            }
        }
    }
    class BookModel
    {
        private int fId;

        public int FId
        {
            get { return fId; }
            set { fId = value; }
        }
        private string fName;

        public string FName
        {
            get { return fName; }
            set { fName = value; }
        }
        private int? fYearPublished;

        public int? FYearPublished
        {
            get { return fYearPublished; }
            set { fYearPublished = value; }
        }
        private int? fCategoryID;

        public int? FCategoryID
        {
            get { return fCategoryID; }
            set { fCategoryID = value; }
        }
        private bool? isDel;

        public bool? IsDel
        {
            get { return isDel; }
            set { isDel = value; }
        }
        private string fAuthor;

        public string FAuthor
        {
            get { return fAuthor; }
            set { fAuthor = value; }
        }
    }
}
没有修改跟删除,这俩功能你自己写吧,很简单
2013-04-24 04:35
快速回复:如何读取数据库里面的信息!求大神!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.045656 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved