Computechsoft Ответов: 1

Правильно выполнить с VB.NET код


Запустите это приложение правильно из vb.net код
при сборке этого приложения и компиляции Exe файла
Запустить файл EXE ошибку :
Cannot open database "COLORS". User login failed for user "Administrator"

пожалуйста, проверьте копию прошлого, чтобы у вас была своя система
так что ясная проблема

Что я уже пробовал:

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

Richard Deeming

Репост
Вы уже разместили это сообщение и получили ответ:
https://www.codeproject.com/Questions/1261063/Run-time-from-VB-NET-no-error[^]

1 Ответов

Рейтинг:
8

OriginalGriff

Посмотрите на сообщение об ошибке:

User login failed for user "Administrator"
Это вполне объяснимо.
Вам нужно поговорить с администратором БД и выяснить, какая комбинация имени пользователя и пароля вам нужна для вашей строки подключения на самом деле - это очень маловероятно "User ID=Administrator;Password=Admin"


Computechsoft

Как поговорить с администратором БД
вы можете сказать любую ссылку так что поговорите с ним

OriginalGriff

Хм. Вы понимаете, что есть кто-то, кто "присматривает" за каждой установкой БД, или должен быть? Нет, я не могу дать вам ссылку на парня, ответственного за вашу конкретную установку SQL Server ...