C#的一些知识

1、将小数截取到某位小数

string  str=3.1415926

string str1=Math.Round(decimal.Parse(str),2) ;//str1为3.14

2、SQL语句操作数据库

    (1)向数据库中插入或更新

                    SqlConnection myCon = new SqlConnection();
                    myCon.ConnectionString = "Data Source=.;Initial Catalog=JSManager;Integrated Security=True";
                    myCon.Open();

                    SqlCommand myCmd = null;

                    strSql += " INSERT……”

                    myCmd = new SqlCommand(strSql, myCon);
                    myCmd.ExecuteNonQuery();

                    myCon.Close();

   (2)从数据库中查询

                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = myCon;
                    cmd.CommandText = "Select LogName from TAB_TeacherInfo where Name = '" + teacherName + "'";
                    cmd.CommandType = CommandType.Text;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.MissingMappingAction = MissingMappingAction.Passthrough;
                    DataTable dt = new DataTable();
                    da.Fill(dt);//这样数据就存在了datatable中

   需要使用时即可从datatable中取, 如dt.Rows[0]["LogName"].ToString()取的即是表中LogName列;dt.Rows.Count得到数据的条数。

posted @ 2012-12-26 14:45  晴心  阅读(193)  评论(0编辑  收藏  举报