从代码VB.NET正确运行 [英] Run correctly from code VB.NET

查看:61
本文介绍了从代码VB.NET正确运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从vb.net代码正确运行此应用程序

构建此应用程序并编译Exe文件

运行Exe文件错误显示:

无法打开数据库COLORS。用户登录失败,用户管理员



请检查复制过去,以便你自己的系统

这样明确的问题



我的尝试:



  Imports  System.Data.SqlClient 
Imports System.Configuration
Imports System.IO

Public FrmColors

私有 Sub Form1_Load(< span class =code-keyword> ByVal sender As System。 Object ByVal e As System.EventArgs)句柄 MyBase .Load

Dim SQLConnectionString As String = ConfigurationManager.ConnectionStrings( RMS.My.MySettings.StupConnection)。ConnectionString
System.Configuration.ConfigurationManager.AppSettings。获取 SQLSqriptRead

CmdRefresh.Enabled = False
PictureImage.SizeMode = PictureBoxSizeMode.CenterImage
PictureImage.Image = 全局 .Colors.My.Resources.Colorful
.Icon = 全球 .Colors.My.Resources.Color_line_Icon

使用 SqlCon As SqlConnection = < span class =code-keyword>新的 SqlConnection(SQLConnectionString)
Dim StrDatabase As String

Dim CreateDirectory As DirectoryInfo = Directory .CreateDirectory(Application.StartupPath& \ Colol

Dim ColorsDataBaseMDF As String = Colors.mdf
Dim ColorsDataBaseLOG 作为 字符串 = 颜色。 log

StrDatabase = 在主服务器上创建数据库颜色(NAME = SQLSqript_Data ,FILENAME ='& Application.StartupPath& \ Colors \& ColorsDataBaseMDF& ',& _
SIZE = 2MB,MAXSIZE = 10MB,FILEGROWTH = 10%)登录(NAME = SQLSqript_Log,FILENAME ='& Application.StartupPath& \ Colors \& ColorsDataBaseLOG& ',& _
SIZE = 1MB,MAXSIZE = 5MB,FILEGROWTH = 10%)

Dim cmd As SqlCommand = SqlCommand(StrDatabase,SqlCon)
尝试
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
SqlCon.Close()
Catch
MsgBox( 记录已存在,删除并刷新以继续:,MsgBoxStyle.Critical)
结束 尝试
结束 使用

D. im SQLConnectionStringTeble 作为 字符串 = 数据源= SYSTEM-PC\SQLEXPRESS;初始目录=颜色;用户ID =管理员;密码=管理员

使用 SqlConTable 作为 SqlConnection = SqlConnection(SQLConnectionStringTeble)

尝试
Dim StrTable 作为 字符串

' ****************************新表颜色创建********** ***************************

StrTable = CREATE TABLE [dbo]。[Colo rRange]([SRNO] [nvarchar](50)NULL,[颜色] [nvarchar](50)NULL,[Palettes] [nvarchar](50)NULL)ON [PRIMARY]
< span class =code-keyword> Dim cmdTable As SqlCommand = SqlCommand(StrTable) ,SqlConTable)

cmdTable.Connection.Open()
cmdTable.ExecuteNonQuery()

' ****************************在脚本表中插入记录******* ******************************
Dim First_Query 作为 SqlCommand( INSERT [dbo]。[ColorRange]([SRNO],[Color],[Palettes])VALUES(N'1',N'White',N'Maroon') ,SqlConTable)
First_Query.ExecuteNonQuery()

Dim Second_Query As SqlCommand( INSERT [dbo]。[ColorRange]([SRNO],[Color],[Palettes])VALUES(N'2' ,N'Red',N'Purple'),SqlConTable)
Second_Query.ExecuteNonQuery()

' ****************************显示DataGridView1中的记录******* ************************************

Dim ds_Colors = DataSet
Dim tables = ds_Colors.Tables

Dim da_Colors = SqlClient.SqlDataAdapter(< span class =code-string> 选择SRNO,颜色,苍白来自COLORS.dbo.ColorRange的订单来自SRNO,SqlConTable)
da_Colors.Fill(ds_Colors, ColorRange

Dim ViewColors As < span class =code-keyword>新 DataView(表( 0 ))
DataGridView1.DataSource = ViewColors

DataGridView1.AllowUserToAddRows = False
DataGridView1。 ReadOnly = True
DataGridView1.Rows( 0 )。单元格( 0 ).Selected = False
SqlConTable.Close()

Catch
' MsgBox(已安装的数据库,MsgBoxStyle.Critical)
结束 尝试
结束 使用

结束 Sub

私有 Sub DataGridView1_CellFormatting( ByVal sender 正如 对象 ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs)句柄 DataGridView1.CellFormatting
DataGridView1.Item(e.ColumnIndex,e.RowIndex).ToolTipText = 这些行已冻结
结束 Sub

私有 Sub CmdRefresh_Click( ByVal sender 正如系统。对象 ByVal e As System.EventArgs)句柄 CmdRefresh.Click
CmdDelete.Enabled = True
CmdRefresh.Enabled = False

Dim SQLConnectionStringTable 作为 字符串 = 数据源= SYSTEM-PC\SQLEXPRESS;初始目录=颜色;用户ID =管理员;密码=管理员

使用 SqlConTable 作为 SqlConnection = SqlConnection(SQLConnectionStringTable)

' ********************** ******在脚本表中插入记录*************************************
SqlConTable.Open()

Dim First_Query As SqlCommand( INSERT [dbo]。[ColorRange] ([SRNO],[Color],[Palettes])VALUES(N'1',N'White',N'Maroon'),SqlConTable)
First_Query.ExecuteNonQuery()

Dim Second_Query 作为 SqlCommand( INSERT [dbo]。[ ColorRange]([SRNO],[Color],[Palettes])VALUES(N'2',N'Red',N'Purple'),SqlConTable)
Second_Query.ExecuteNonQuery()
' ********************** ******插入过程完成***************************************** **

Dim ds_Colors = DataSet
Dim tables = ds_Colors.Tables

Dim da_Colors = SqlClient.SqlDataAdapter( 从COLORS.dbo中选择SRNO,颜色,调色板.SolorRange order by SRNO,SqlConTable)
da_Colors.Fill(ds_Colors, ColorRange

Dim Vi ewColors 作为 DataView(表格( 0 ))
DataGridView1.DataSource = ViewColors

DataGridView1.AllowUserToAddRows = False
DataGridView1。 ReadOnly = True
DataGridView1.Rows( 0 )。单元格( 0 )。Selected = False
SqlConTable.Close()

结束 使用
结束 Sub

私有 Sub CmdDelete_Click( ByVal sender As System。对象 ByVal e As System.EventArgs)句柄 CmdDelete.Click
CmdRefresh.Enabled = True
CmdDelete.Enabled = False
DataGridView1.DataSource = Nothing

Dim SQLConnectionStringTable As String = 数据源= SYSTEM-PC\SQLEXPRESS;初始目录=颜色;用户ID =管理员;密码=管理员

使用 SqlConTable 作为 SqlConnection = SqlConnection( SQLConnectionStringTable)
SqlConTable.Open()
Dim DeleteColorRange As SqlCommand( 从COLORS.dbo.ColorRange中删除,SqlConTable)
DeleteColorRange.ExecuteNonQuery()
结束 使用

结束 Sub

私有 Sub CmdDelete_MouseHover( ByVal sender As Object ByVal e As System.EventArgs)句柄 CmdDelete.MouseHover
CmdDelete.ForeColor = Color.Fuchsia
结束 Sub

Private Sub CmdDelete_MouseLeave( ByVal 发​​件人作为 对象 ByVal e As System.EventArgs)句柄 CmdDelete.MouseLeave
CmdDelete.ForeColor = Color.Black
结束 Sub

私有 Sub DataGridView1_CellContentClick( ByVal sender As System。 Object ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.C ellContentClick
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End Sub

结束

解决方案

查看错误消息:

用户Administrator用户登录失败

这是非常自我解释的。

您需要与数据库管理员交谈,并找出您的连接字符串实际所需的用户名和密码组合 - 它不太可能是用户ID =管理员;密码=管理员


Run this application correctly from vb.net code
when build this application and compile Exe file
Run Exe file error display:
Cannot open database "COLORS". User login failed for user "Administrator"

please check copy past so that you's own system
so that clear problem

What I have tried:

Imports System.Data.SqlClient
Imports System.Configuration
Imports System.IO

Public Class FrmColors

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim SQLConnectionString As String = ConfigurationManager.ConnectionStrings("RMS.My.MySettings.StupConnection").ConnectionString
        System.Configuration.ConfigurationManager.AppSettings.Get("SQLSqriptRead")

        CmdRefresh.Enabled = False
        PictureImage.SizeMode = PictureBoxSizeMode.CenterImage
        PictureImage.Image = Global.Colors.My.Resources.Colorful
        Me.Icon = Global.Colors.My.Resources.Color_line_Icon

        Using SqlCon As SqlConnection = New SqlConnection(SQLConnectionString)
            Dim StrDatabase As String

            Dim CreateDirectory As DirectoryInfo = Directory.CreateDirectory(Application.StartupPath & "\Colors")

            Dim ColorsDataBaseMDF As String = "Colors.mdf"
            Dim ColorsDataBaseLOG As String = "Colors.log"

            StrDatabase = "CREATE DATABASE COLORS ON PRIMARY (NAME = SQLSqript_Data, FILENAME = '" & Application.StartupPath & "\Colors\" & ColorsDataBaseMDF & "', " & _
           " SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) LOG ON(NAME = SQLSqript_Log, FILENAME = '" & Application.StartupPath & "\Colors\" & ColorsDataBaseLOG & "'," & _
           " SIZE = 1MB,MAXSIZE = 5MB, FILEGROWTH = 10%) "

            Dim cmd As SqlCommand = New SqlCommand(StrDatabase, SqlCon)
            Try
                cmd.Connection.Open()
                cmd.ExecuteNonQuery()
                cmd.Connection.Close()
                SqlCon.Close()
            Catch
                MsgBox("Record already exist, delete and refresh to continue :", MsgBoxStyle.Critical)
            End Try
        End Using

        Dim SQLConnectionStringTeble As String = "Data Source=SYSTEM-PC\SQLEXPRESS;Initial Catalog=COLORS;User ID=Administrator;Password=Admin"

        Using SqlConTable As SqlConnection = New SqlConnection(SQLConnectionStringTeble)

            Try
                Dim StrTable As String

                '**************************** New table "Color" create *************************************

                StrTable = "CREATE TABLE [dbo].[ColorRange]([SRNO] [nvarchar](50) NULL,[Color] [nvarchar](50) NULL,[Palettes] [nvarchar](50) NULL) ON [PRIMARY]"
                Dim cmdTable As SqlCommand = New SqlCommand(StrTable, SqlConTable)

                cmdTable.Connection.Open()
                cmdTable.ExecuteNonQuery()

                '**************************** Insert record in script table *************************************
                Dim First_Query As New SqlCommand("INSERT [dbo].[ColorRange] ([SRNO], [Color], [Palettes]) VALUES (N'1', N'White', N'Maroon')", SqlConTable)
                First_Query.ExecuteNonQuery()

                Dim Second_Query As New SqlCommand("INSERT [dbo].[ColorRange] ([SRNO], [Color], [Palettes]) VALUES (N'2', N'Red', N'Purple')", SqlConTable)
                Second_Query.ExecuteNonQuery()

                '**************************** Display record in DataGridView1 *******************************************

                Dim ds_Colors = New DataSet
                Dim tables = ds_Colors.Tables

                Dim da_Colors = New SqlClient.SqlDataAdapter("Select SRNO,Color,Palettes from COLORS.dbo.ColorRange order by SRNO", SqlConTable)
                da_Colors.Fill(ds_Colors, "ColorRange")

                Dim ViewColors As New DataView(tables(0))
                DataGridView1.DataSource = ViewColors

                DataGridView1.AllowUserToAddRows = False
                DataGridView1.ReadOnly = True
                DataGridView1.Rows(0).Cells(0).Selected = False
                SqlConTable.Close()

            Catch
                'MsgBox(" Already installed database", MsgBoxStyle.Critical)
            End Try
        End Using

    End Sub

    Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
        DataGridView1.Item(e.ColumnIndex, e.RowIndex).ToolTipText = "These rows has been freeze"
    End Sub

    Private Sub CmdRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdRefresh.Click
        CmdDelete.Enabled = True
        CmdRefresh.Enabled = False

        Dim SQLConnectionStringTable As String = "Data Source=SYSTEM-PC\SQLEXPRESS;Initial Catalog=COLORS;User ID=Administrator;Password=Admin"

        Using SqlConTable As SqlConnection = New SqlConnection(SQLConnectionStringTable)

            '**************************** Insert record in script table *************************************
            SqlConTable.Open()

            Dim First_Query As New SqlCommand("INSERT [dbo].[ColorRange] ([SRNO], [Color], [Palettes]) VALUES (N'1', N'White', N'Maroon')", SqlConTable)
            First_Query.ExecuteNonQuery()

            Dim Second_Query As New SqlCommand("INSERT [dbo].[ColorRange] ([SRNO], [Color], [Palettes]) VALUES (N'2', N'Red', N'Purple')", SqlConTable)
            Second_Query.ExecuteNonQuery()
            '**************************** Insert process complete *******************************************

            Dim ds_Colors = New DataSet
            Dim tables = ds_Colors.Tables

            Dim da_Colors = New SqlClient.SqlDataAdapter("Select SRNO,Color,Palettes from COLORS.dbo.ColorRange order by SRNO", SqlConTable)
            da_Colors.Fill(ds_Colors, "ColorRange")

            Dim ViewColors As New DataView(tables(0))
            DataGridView1.DataSource = ViewColors

            DataGridView1.AllowUserToAddRows = False
            DataGridView1.ReadOnly = True
            DataGridView1.Rows(0).Cells(0).Selected = False
            SqlConTable.Close()

        End Using
    End Sub

    Private Sub CmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdDelete.Click
        CmdRefresh.Enabled = True
        CmdDelete.Enabled = False
        DataGridView1.DataSource = Nothing

        Dim SQLConnectionStringTable As String = "Data Source=SYSTEM-PC\SQLEXPRESS;Initial Catalog=COLORS;User ID=Administrator;Password=Admin"

        Using SqlConTable As SqlConnection = New SqlConnection(SQLConnectionStringTable)
            SqlConTable.Open()
            Dim DeleteColorRange As New SqlCommand("Delete from COLORS.dbo.ColorRange", SqlConTable)
            DeleteColorRange.ExecuteNonQuery()
        End Using

    End Sub

    Private Sub CmdDelete_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmdDelete.MouseHover
        CmdDelete.ForeColor = Color.Fuchsia
    End Sub

    Private Sub CmdDelete_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmdDelete.MouseLeave
        CmdDelete.ForeColor = Color.Black
    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    End Sub

End Class

解决方案

Look at the error message:

User login failed for user "Administrator"

It's pretty self explanatory.
You need to talk to the DB admin and find out what the username and password combination you need for your connection string actually is - it's very unlikely to be "User ID=Administrator;Password=Admin"


这篇关于从代码VB.NET正确运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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