使用vb.net在SQL Server中存储文本文件数据 [英] Storing text file data in SQL server using vb.net

查看:91
本文介绍了使用vb.net在SQL Server中存储文本文件数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将数据(txt格式)存储在SQL Server数据库中,但是当我为它创建虚拟目录时,它仅在我的系统(数据库和代码所在的系统)上工作.

我正在vb.net中编写此代码,请检查是否有任何错误.


设计文件

I m trying to store my data(txt format)in SQL server database but when I created virtual directory for it, its working only on my sytem (on which my database and the code exist).

I am writing this code in vb.net, please check for any error.


Design file

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="readingtext.aspx.vb" Inherits="readingtext" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <%--<b>Raw File Output</b><br />--%>
    <center>
<asp:label id="LblMsg" runat="server" />
<br />
<asp:TextBox  ID="txtbox1" runat="server" Text=""></asp:TextBox>
<%--<p>
<u><b>Data Of Text File</b></u><br /></p>
<asp:label runat="server" id="lblNicerOutput" Font-Name="Verdana" />--%>

<br />
<asp:Button ID="Btn1" Text="PostTosql" runat="server" />
</center>
    </div>
    </form>
</body>
</html>







代码文件:







code file:

Imports System.Data.SqlClient
Imports System.Data.Sql
Imports System.Web.UI

Imports System.Web.UI.WebControls
Imports System.Text
Imports System.Configuration
Imports System.Xml
Imports System.Data
Imports System.Drawing
Imports System.IO
Partial Class readingtext
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'If Not IsPostBack Then
        '    LblMsg.Text = String.Empty
        '    txtbox1.Text = String.Empty
        'End If



    End Sub

    Protected Sub Btn1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Btn1.Click
        Try
            Dim objSRFile As StreamReader
            Dim strInput, arrStrInput() As String
            Dim intCurrPos As Integer
            Dim strName As String
            Dim Age As String
            Dim RollNo As String
            strName = String.Empty
            Age = String.Empty
            RollNo = String.Empty
            'Dim intSalary As Integer
            'DB vars
            Dim strSQL As String
            Dim strConnString As String = ConfigurationManager.ConnectionStrings("MyCon").ConnectionString()
            Dim Con As New SqlConnection(strConnString)
            Dim objCmd As SqlCommand
            File.OpenText(txtbox1.Text)
            objSRFile = File.OpenText(txtbox1.Text)
            ' objSRFile = File.OpenText("x:\path\to\file.txt")
            While objSRFile.Peek <> -1
                strInput = objSRFile.ReadLine
                arrStrInput = Split(strInput, ",", , CompareMethod.Text)
                For intCurrPos = 0 To arrStrInput.Length - 1
                    Select Case intCurrPos
                        Case 0
                            strName = arrStrInput(intCurrPos)
                        Case 1
                            Age = arrStrInput(intCurrPos)
                        Case 2
                            RollNo = arrStrInput(intCurrPos)
                    End Select
                Next
                strSQL = "insert into InsertDataNew(Name,age,RollNo) values('" & strName & "', '" & Age & "', '" & RollNo & "')"
                objCmd = New SqlCommand(strSQL, Con)
                Con.Open()
                objCmd.ExecuteNonQuery()
                LblMsg.Text = "Data has Entered Successfully"
                Con.Close()
            End While
            objSRFile.Close()
        Catch ex As Exception
            Trace.Write(ex.Message)
        End Try
        'File to string vars
    End Sub
End Class

推荐答案

所有权限均已正确分配,
但它仍无法正常工作.
All permissions are assigned properly ,
but it is not working yet.


将其部署到另一台计算机时是否有问题?我想你是说这是问题.

如果它在本地运行,请确保在目标部署环境中,Web服务器所运行的帐户在您的数据目录中具有读/写权限.

干杯.
Do you have a problem when you deploy it to another machine? I think you''re saying this is the problem.

If it works locally, make sure the account that the web server runs under has read/write permission in your data directory in the target deployment environment.

Cheers.


这篇关于使用vb.net在SQL Server中存储文本文件数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆