.net总结(二)

来源:互联网 发布:擎洲广达计价软件 编辑:程序博客网 时间:2024/06/05 18:59

一、时间格式化
后台:

//先转换为DateTime类型,再转为stringpub_time.Text=(Convert.ToDateTime( (dataset.Tables[0].Rows[0][2])).GetDateTimeFormats('D')[0]).ToString();

前台:直接获取数据

//从数据库获取的时间<%#Eval("pub_time")%>

当然,时间格式化还有很多形式,上网一搜就能搜到很多,这里推荐一篇博客,里面讲的还是挺全面的。
https://www.cnblogs.com/dongqi/archive/2009/04/01/1426827.html
二、SQL语句参数很多,可以用AddWithValue()赋值

        //第一步:写sql语句    @username(@也可以用其他的字符来代替,只要复制的时候的参数和这个是一样的就行)                String sql = "select * from tb_user where username=@username and password=@password";                //第二步:创建数据库连接                con= DBConnection.GetConnection();                con.Open();                //第三步:创建MySQLCommand对象                command=new MySqlCommand(sql,con);                //对参数赋值   AddWithValue(参数名,参数值)                              command.Parameters.AddWithValue("@username", username);                command.Parameters.AddWithValue("@password", password);

三、发布新闻成功,但是数据库存储内容与发布的内容不相符
发布内容:
这里写图片描述
数据库存储:(都是InputText)
这里写图片描述
找了很久的问题,后来才发现是SQL语句赋值的地方并没有把获取的文本框里的值赋上去,而是直接用的字段名

string title1=title.Value;string type1 = type.Value;//这里不应该是title,而是title1 ,所以在写的过程中一定要细心string sql = "insert into tb_news(title,type,key_word,content,pub_time,file_name) values('"+title+ "','" + type + "','" + key_word + "','" + content+ "','" + pub_time1 + "','" + file_name1 + "')";

四、分页
之前写Java分页感觉很麻烦(可能是自己还太渣了,哈哈),在.net 里面有一个DataPager的控件,这个不用自己写分页功能了。

  • 第一步:在工具箱中找到DataPage控件
    这里写图片描述

  • 第二步:将控件拖至页面中
    这里写图片描述

  • 第三步:选择导航形式
    这里写图片描述

  • 第四步:在属性中选择页数
    这里写图片描述

五、数据库存储中文出现乱码

这里写图片描述
连接数据库的代码:

//获取数据库连接        public static MySqlConnection GetConnection()        {            string connection = "server=localhost;user id=xxx;password=xxx;database=news_system; pooling=true;";            MySqlConnection conn = new MySqlConnection(connection);            return conn;        }   

刚开始在connection 变量里面加了charset=utf-8;结果运行报错,说不认识utf-8

string connection = "server=localhost;user id=root;password=1234;database=news_system; pooling=true;charset=utf-8";

这里写图片描述
后来就上网查说要改成charset=gbk;这样就没有问题了。

原创粉丝点击