ASP.NET文件上传范例

Standard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<%@ Import Namespace="System" %>  
<%@ Import Namespace="System.Web.HttpPostedFile" %>  
<%@ Import Namespace="System.Web.UI.HtmlControls.HtmlInputFile" %>  
<%
  'www.yemaosheng.com
  Response.expires   =   0      
  Response.addHeader("pragma","no-cache")    
  Response.addHeader("cache-control","private")
  Response.CacheControl   =   "no-cache"
  dim i=0
  if request.queryString("state")="" then
    For i=0 to Request.Files.Count-1  
      '使用Request.Files()来逐个获取上传的文件  
      dim myFile as HttpPostedFile=Request.Files(i)  
      response.write(request.Form("text_desc"))
      myFile.SaveAs(Server.MapPath(".") & "\upload\" & myFile.FileName)
    Next
  end if
%>
<form action="" method="post" enctype="multipart/form-data" name="form_file" id="form_file">
  <textarea name="text_desc" id="text_desc"></textarea>
  <input type="file" name="filename">
  <input type="hidden" name="state" value="1">
  <input type="hidden" name="site" value="yemaosheng.com">
  <input type="submit" name="Submit" value="Submit">
</form>

VB.NET调用WebService时报错

Standard

加入wsDataContent.Credentials = System.Net.CredentialCache.DefaultCredentials即正常(*号标出)

‘WebService源文件
Private dsData As DataSet

Private Sub LoadDataSet()

Dim successful As Boolean
successful = True

Dim errorMessage As String
errorMessage = “”

dsData = New DataSet
dsData.DataSetName = “db”

Try
Dim dbConn As New OleDbConnection
With dbConn
.ConnectionString = “PROVIDER=SQLOLEDB;Server=(local);DATABASE=db_name;UID=sa;PWD=;”
.Open()
End With

Try
Dim dbCmdUser As New OleDbCommand
dbCmdUser.Connection = dbConn
dbCmdUser.CommandText = “SELECT * FROM tb_user”
Dim dbCmdProject As New OleDbCommand
dbCmdProject.Connection = dbConn
dbCmdProject.CommandText = “SELECT * FROM tb_project”
Dim dbCmdJob As New OleDbCommand
dbCmdJob.Connection = dbConn
dbCmdJob.CommandText = “SELECT * FROM tb_job”
Dim dbCmdTopic As New OleDbCommand
dbCmdTopic.Connection = dbConn
dbCmdTopic.CommandText = “SELECT * FROM tb_topic”

Dim dbAdUser As New OleDbDataAdapter
dbAdUser.SelectCommand = dbCmdUser
Dim dbAdProject As New OleDbDataAdapter
dbAdProject.SelectCommand = dbCmdProject
Dim dbAdJob As New OleDbDataAdapter
dbAdJob.SelectCommand = dbCmdJob
Dim dbAdTopic As New OleDbDataAdapter
dbAdTopic.SelectCommand = dbCmdTopic

dbAdUser.Fill(dsData, “User”)
dbAdProject.Fill(dsData, “Project”)
dbAdJob.Fill(dsData, “Job”)
dbAdTopic.Fill(dsData, “Topic”)
Catch ex A* **ception
successful = False
errorMessage = ex.Message.ToString
errorMessage = “Error reading data.”
Finally
dbConn.Close()
End Try
Catch ex A* **ception
successful = False
errorMessage = “Error opening data connention.”
End Try

If Not successful Then
Throw New Exception(errorMessage)
End If
End Sub

<WebMethod()> _
Public Function getDataSet() As DataSet
LoadDataSet()
Return dsData
End Function

‘VB.NET窗体源文件
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Dim wsDataContent As DataContent
wsDataContent = New DataContent
‘***********www.yemaosheng.com***********’
wsDataContent.Credentials = System.Net.CredentialCache.DefaultCredentials
Dim ds As DataSet
ds = wsDataContent.getDataSet()
dgMain.DataSource = ds
dgMain.DataMember = “User”
Catch ex A* **ception
MsgBox(ex.Message, , “出错提示”)
End Try
End Sub

ASP.NET调用DLL范例

Standard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
'---------------------------------------------
'数据库链接DLL www.yemaosheng.com
'---------------------------------------------
Imports System.Data
Imports System.Data.OleDb
Public Class Conn
    Dim myConnection As OleDbConnection
    Dim myCommand As OleDbCommand
    Dim myDataadapter As SqlDataAdapter
    Dim myDatareader As OleDbDataReader
    Dim myDataset As DataSet
    '连接数据库
    Public Function Conection(ByVal strConn)
        myConnection = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=" & strConn)
        myConnection.Open()
    End Function
 
    '执行Select将结果放入DataSet并返回
    Public Function ExecuteToDataAdapter(ByVal strSql, ByVal tb) As DataSet
        myCommand = New SqlCommand(strSql, myConnection)
        myDataadapter = New SqlDataAdapter(myCommand)
        myDataset = New DataSet
        myDataadapter.Fill(myDataset, tb)
        Return myDataset
    End Function
 
    '执行Select语句
    Public Function ExecuteQuery(ByVal strSql) As OleDbDataReader
        myCommand = New OleDbCommand(strSql, myConnection)
        myDatareader = myCommand.ExecuteReader()
        Return myDatareader
    End Function
 
    '执行Update和Insert语句
    Public Function ExecuteUpdate(ByVal strSql)
        myCommand = New OleDbCommand(strSql, myConnection)
        myCommand.ExecuteNonQuery()
    End Function
 
    '关闭数据库连接
    Public Function Close()
        myDatareader.Close()
        myConnection.Close()
        myConnection.Dispose()
    End Function
 
    '判断数据库中的字段是否为空
    Public Function DbLen(ByVal Field As Object) As Integer
        Dim len As Integer
        Dim NewVar As String = ""
        Dim ObjType As Type = Field.GetType()
        If ObjType.FullName = "System.DBNull" Then
            NewVar = Field.Value.ToString
        Else
            NewVar = Field
        End If
        Return NewVar.Length
    End Function
End Class
 
'---------------------------------------------
'aspx调用 www.yemaosheng.com
'---------------------------------------------
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="DBConn" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Data" %>
 
<%
Dim conn As New Conn()
Dim dtr As OleDbDataReader
Dim sql As String
conn.Conection(Request.ServerVariables("APPL_PHYSICAL_PATH") & "common\survey.mdb")
If request.Form("radio_survey")<>"" Then
  sql="UPDATE tb_surveyitem SET surveyitem_item" & request.Form("radio_survey") & "=surveyitem_item" & request.Form("radio_survey") & "+1 WHERE surveyitem_survey=" & request.Form("hidden_surveyid")
  conn.ExecuteUpdate(sql)
End If
dtr=conn.ExecuteQuery("select * from tb_survey,tb_surveyitem where survey_id=surveyitem_survey and survey_id=" & request.Form("hidden_surveyid"))
dtr.Read()
%>
 
  <table width="500" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#333333">
  <tr>
    <td><table width="500" border="0" cellspacing="1" cellpadding"0">
      <tr align="center">
        <td colspan="2" bgcolor="#006699" class="title_text1"><strong><%=dtr("survey_title")%></strong></td>
      </tr>
  <%
    Dim I As Integer
    For I = 1 To 6
      If conn.DbLen(dtr("survey_item" & I & "title")) > 0 then
  %>
      <tr>
        <td width="100" bgcolor="#006699" class="title_text1"><%=dtr("survey_item" & I & "title")%></td>
        <td width="400" bgcolor="#ECF9FF" valign=middle>
        <img src='images/left-line.gif' width='<%=dtr("surveyitem_item" & I)*10%>' height=10>
        </td>
      </tr>
  <%
      End If
    Next
  %>
    </table></td>
  </tr>
</table>
<%
dtr.Close()
conn.Close()
%>
'---------------------------------------------
<TABLE cellSpacing=2 cellPadding=2 width=600 bgColor=#ffffff>
  <TBODY>
    <TR class="content_text1">
      <TD bgColor=#bfd5e3 colSpan=6>招聘信息</TD>
    </TR>
    <%
    Dim conn As New Conn()
    Dim dtr As OleDbDataReader
    conn.Conection(Request.ServerVariables("APPL_PHYSICAL_PATH") & "common\hr.mdb")
    dtr=conn.ExecuteQuery("select * from tb_hr where hr_state=1")
    While dtr.Read()
    %>
          <TR class="content_text1">
            <TD width=10% bgColor=#e9e9e9>职位名称:</TD>
            <TD width=50%><%=dtr("hr_job")%></TD>
            <TD width=10% bgcolor="#E9E9E9">性别要求:</TD>
            <TD width=10%><%=dtr("hr_gender")%></TD>
            <TD width=10% bgcolor="#E9E9E9">招聘人数:</TD>
            <TD width=10%><%=dtr("hr_num")%></TD>
          </TR>
          <TR class="content_text1">
            <TD width=10% valign="top" bgColor=#e9e9e9>职位描述:</TD>
            <TD colspan="5"><%=Replace(dtr("hr_desc"),vbCrLf,"<br>")%></TD>
          </TR>
          <TR class="content_text1">
            <TD bgColor=#bfd5e3 colspan="6"> </TD>
          </TR>
    <%
    End While
    dtr.Close()
    conn.Close()
    %>
  </TBODY>
</TABLE>
'---------------------------------------------
Dim conn As New Conn()
Dim ds As DataSet
Dim dr As DataRow
ds = conn.ExecuteToDataAdapter("select * from tb_board","board")
For Each dr In ds.Tables("board").Rows
    response.write(dr("board_id"))
Next

VB.NET更改域用户身份复制文件到域中的共享目录

Standard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
Imports System.Management
Imports System.Security.Principal
 
Module modTemp
 
  Dim WITH_PROFILE As Integer = 5
  Dim LOGON32_PROVIDER_DEFAULT As Integer = 0
  Dim LOGON32_LOGON_INTERACTIVE As Integer = 2
  Const LOGON32_LOGON_NETWORK As Long = 3
 
  Dim impersonationContext As WindowsImpersonationContext
 
  Declare Function LogonUserA Lib "advapi32.dll" (ByVal lpszUsername As String, _
              ByVal lpszDomain As String, _
              ByVal lpszPassword As String, _
              ByVal dwLogonType As Integer, _
              ByVal dwLogonProvider As Integer, _
              ByRef phToken As IntPtr) As Integer
 
  Declare Auto Function DuplicateToken Lib "advapi32.dll" ( _
              ByVal ExistingTokenHandle As IntPtr, _
              ByVal ImpersonationLevel As Integer, _
              ByRef DuplicateTokenHandle As IntPtr) As Integer
 
  Declare Auto Function RevertToSelf Lib "advapi32.dll" () As Long
  Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Long
 
  Public Function impersonateValidUser(ByVal userName As String, ByVal domain As String, _
  ByVal password As String) As Boolean
 
    Dim tempWindowsIdentity As WindowsIdentity
    Dim token As IntPtr = IntPtr.Zero
    Dim tokenDuplicate As IntPtr = IntPtr.Zero
    impersonateValidUse* = **lse
 
    If RevertToSelf() Then
      If LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, _
        LOGON32_PROVIDER_DEFAULT, token) &lt;&gt; 0 Then
        If DuplicateToken(token, 2, tokenDuplicate) &lt;&gt; 0 Then
          tempWindowsIdentity = New WindowsIdentity(tokenDuplicate)
          impersonationContext = tempWindowsIdentity.Impersonate()
          If Not impersonationContext Is Nothing Then
            impersonateValidUser = True
          End If
        End If
      End If
    End If
    If Not tokenDuplicate.Equals(IntPtr.Zero) Then
      CloseHandle(tokenDuplicate)
    End If
    If Not token.Equals(IntPtr.Zero) Then
      CloseHandle(token)
    End If
  End Function
 
  Public Sub undoImpersonation()
    impersonationContext.Undo()
  End Sub
 
End Module
 
  'www.yemaosheng.com
 
  Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click
    If impersonateValidUser("username", "domainname", "password") Then
      Call ToCopyFile()
    Else
      MsgBox("Impersonation failed")
    End If
  End Sub
 
  Private Sub ToCopyFile()
    Try
      Dim strSource As String
      strSource = "C:\Documents and Settings\yemaosheng\桌面\test.txt"
 
      Dim strTarget As String
      strTarget = "\\domain-svr\temp\test_ye.txt"
 
      System.IO.File.Copy(strSource, strTarget, True)
 
    Catch ex A* **ception
      MsgBox(ex.Message.ToString, , "提示")
    End Try
  End Sub