Asp.net学习笔记

来源:互联网 发布:网络棋牌游戏赌博平台 编辑:程序博客网 时间:2024/05/19 00:37

1、在web.config中使用ConnectionStrings节点设置数据库连接字串
<connectionStrings> 
<add name="XXXConnectionString" connectionString=" 连接字串" providerName="System.Data.SqlClient" />
</connectionStrings>
在程序中读取: 
ConfigurationManager.ConnectionStrings("XXXConnectionString").ConnectionString

2、DataSource和GridView的应用
A:设置DataSource的参数(SelectCommand、ConnectionString)
B:GridView1.DataSource=DataSource
C:GridView1.DataBind()

3、Now.ToString("yyMMdd") 格式化日期(如091211)

4、GridView中编辑列中的DataFormString设置日期格式:{0:yyyy-MM-dd}

5、GridView中删除某一列
A:设置GridView的DataKeyNames的主键字
B:通过GridView1.DataKeys(e.RowIndex).Value来取出关键字段的值。
C:用sql删除

6、数据库查询操作范例
Using sqlcn As New SqlClient.SqlConnection(连接字串——获取方法见1)
            sqlcn.Open()
            Dim sqlcm As New SqlClient.SqlCommand
            sqlcm.CommandType = CommandType.Text
            sqlcm.Connection = sqlcn
            sqlcm.CommandText = sql命令
            sqlcm.ExecuteNonQuery()——无返回
            sqlcm.ExecuteScalar()——返回第一行第一列
            sqlcm.ExecuteReader()——返回SqlClient.SqlDataReader
            sqlcn.Close()
        End Using

7、css中的id和class表示方法
id:#id名{}
class:#class名{}

8、IsDBNull()和Is Nothing在ExecuteScalar中的用法区别
IsDBNull用于判断字符型和日期型的字段是否为DbNull,与ExecuteScalar().ToString=""等价
Is Nothing则是用于判断数值型的字段。
【转摘】:
在对象没有被创建时(没有被new时),这个对象is nothing ,将nothing赋值给普通类型,该类型初始化;赋值给对象类型,对象类型没有关联对象。
IsDBNull()来确定变量是否进行了初始化。IsDBNull方法接受一个对象作为它的参数,并返回一个布尔值,表明变量是否被初始化。
判断字符串为空可用xxx.ToString.Length 或者String.IsNullOrEmpty("xxxx")

9、使用System.Net.Mail发送带SMTP验证的邮件
Protected Sub SendMail(ByVal strTo As String, ByVal strFrom As String, ByVal strSubject As String, ByVal strBody As String)
        Dim MM As New System.Net.Mail.MailMessage(strFrom, strTo, strSubject, strBody)
        Dim sc As New System.Net.Mail.SmtpClient("邮件主机", 端口25)
        Dim mmc As New System.Net.NetworkCredential("发送邮件账号", "发送邮件密码")
        sc.Credentials = mmc
        sc.Send(MM)
    End Sub

10、GridView控件的RowCommand、RowDeleting和RowEditing事件中取某行键字段的值

A:先在GridView属性中设置DataKeyNames,可用“,”分割。

B:在RowCommand事件程序中用GridView1.DataKeys(CInt(e.CommandArgument)).Value来获取

在RowDeleting事件程序中用GridView1.DataKeys(e.RowIndex).Value来获取

在RowEditing事件程序中用GridView1.DataKeys(e.NewEditIndex).Value来获取

0 0
原创粉丝点击