I am facing a problem that when i make the setup file of my application developed in vb.net the database file will not be attached to it, as a solution i wish to create the database # first run of the application through the following code
Dim myCommand As String
Dim myConnectionString As SqlConnection = New SqlConnection("Data Source=(local)\SQLEXPRESS;Initial Catalog=soft_billing;Integrated Security=True;Pooling=False")
myCommand = "CREATE database soft_billing"
Dim cmd As SqlCommand = New SqlCommand(myCommand, myConnectionString)
Try
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
Catch
MsgBox(" Already installed database", MsgBoxStyle.Critical, " MaS InfoTech- Warning")
End Try
it will not generated the required data base,
help me please, thanks in advane
Dim myCommand As String
Dim myConnectionString As SqlConnection = New SqlConnection("Data Source=(local)\SQLEXPRESS;Initial Catalog=master;Integrated Security=True;Pooling=False")
myCommand = "CREATE database soft_billing"
Dim cmd As SqlCommand = New SqlCommand(myCommand, myConnectionString)
Try
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
Catch
MsgBox(" Already installed database", MsgBoxStyle.Critical, " MaS InfoTech- Warning")
End Try
Related
Hey I'm trying to use this code to save to my access database and although there are no errors and it says data saved, my access database doesn't receive the new data. What's wrong with it?
Private Sub savenew()
Dim conn As New OleDbConnection
conn = New OleDbConnection
dbprovider = "Provider=Microsoft.ACE.OLEDB.12.0;"
dbsource = "Data Source = FULL YUGIOH ACCESS DATABASE.accdb;"
conn.ConnectionString = dbprovider & dbsource
Dim reader As OleDbDataReader
Dim command As OleDbCommand
Try
conn.Open()
Dim query As String
query = "insert into item(name) values ('" & TextBox4.Text & "')"
command = New OleDbCommand(query, conn)
reader = command.ExecuteReader
MsgBox("data saved")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
(table in my database is called item, and column is called name.)
Dim conn As MysqlConnection
conn = New Mysqlconnection()
conn.ConnectionString = "server=justhost.com; user id=*****; password=****; database=login"
Try
conn.Open()
Catch myerror As MySqlException
MsgBox("Error connecting to database!")
End Try
this code not working ? how to connect online phpmyadmin database use vb.net?
Have you tried installing this one?
http://dev.mysql.com/downloads/connector/net/
and after installing that, try to reconfigure your code try this.
Imports MySql.Data.MySqlClient
Dim Connection As MySqlConnection = New MySqlConnection()
Dim ConnectionString As String = "Server=SERVERNAME or the Database online link;Database=DATABASE;Uid=root;Pwd=password;"
Connection.ConnectionString = ConnectionString
Connection.Open()
//Execute here what ever you want just follow your heart <3
myConnection.Close()
When I try to connect to AcessDB, I get the error
"Keyword is not supported: provider".
When I try to change provider, I get another error. When I delete provider tag, I also get an error.
Dim Exists As Boolean = False
Dim ConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Name\Documents\Visual Studio 2010\Projects\datagrid\datagrid\pokus.accdb;Persist Security Info=False;"
Dim connection As New SqlConnection(ConnectionString)
Try
connection.Open()
Dim command As SqlCommand = connection.CreateCommand
command.CommandText = "SELECT * FROM studenti"
Dim reader As SqlDataReader = command.ExecuteReader
If reader.HasRows Then
Exists = True
Else
Exists = False
End If
reader.Close()
command.Dispose()
Catch ex As Exception
Console.Write(ex.Message)
Finally
connection.Close()
End Try
SqlConnection, SqlCommand, etc. are SQL Server-specific classes. You can't use them to connect to MS Access.
The documentation for SqlConnection makes this very clear:
Represents an open connection to a SQL Server database.
Consider using the OldDbConnection and related classes instead.
I always write wrapper classes for database connections, and I always recommend the same.
Dim sConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Name\Documents\Visual Studio 2010\Projects\datagrid\datagrid\pokus.accdb;Persist Security Info=False;"
Dim Conn As New OleDbConnection
Conn.ConnectionString = sConnectionString
Conn.Open()
Dim sQuery As String = "SELECT * FROM studenti"
Dim da As New OleDbDataAdapter(sQuery, Conn)
Dim dt As New DataTable
da.Fill(dt)
Conn.Close()
Dim rpt As New CheckoutReportpreview
Dim cn As SqlConnection
Dim cmd As New SqlCommand
Dim myDA As New SqlDataAdapter
Dim ds As New CheckoutSet
Dim crConnectionInfo As New ConnectionInfo
Try
With crConnectionInfo
.ServerName = "."
.DatabaseName = "Hotel Management System"
.UserID = "sa"
.Password = "s123"
End With
cmd.Connection = cn
cn.Open()
Dim da As New SqlDataAdapter("select * from tblCheckout where CheckoutDate>='" & CDate(Date_from.Value) & "' and CheckoutDate<='" & CDate(Date_to.Value) & "' ", cn)
cmd.Prepare()
cmd.CommandType = CommandType.Text
myDA.SelectCommand = cmd
myDA.Fill(ds, "tblCheckout")
rpt.SetDataSource(ds)
CheckoutReport.Show()
CheckoutReport.CrystalReportViewer1.ReportSource = rpt
cn.Close()
Catch Excep As Exception
MessageBox.Show(Excep.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
I HAVE A CODE HERE AND I WANT TO DISPLAY THE REPORT BETWEEN TO DATES TO OTHER FORM TO CLICK PREVIEW BUTTON, SO CAN YOU HELP ME WHEN I RUN THE MESSAGE APPEAR IS
object reference not set to an instance of an object
To answer your question on why you're getting a "object reference not set to an instance of an object" look at your SqlConnection. You declare it but never instantiate it so it's null/nothing when you reference it giving you that error. Your probably getting it on the cn.Open() line.
Change:
Dim cn As SqlConnection
to something like:
Dim cn As New SqlConnection("server=yourserver;database=yourdatabase;uid=sa=pwd=sa123")
I have a more data in excel file .so i have to import it into sql database using vb.net.can anyone send the source code?
Dim ExcelConnection As New
System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\MyExcelSpreadsheet.xlsx; Extended Properties=""Excel 12.0 Xml; HDR=Yes""")
ExcelConnection.Open()
Dim expr As String = "SELECT * FROM [Sheet1$]"
Dim objCmdSelect As OleDbCommand = New OleDbCommand(expr, ExcelConnection)
Dim objDR As OleDbDataReader
Dim SQLconn As New SqlConnection()
Dim ConnString As String = "Data Source=MMSQL1; Initial Catalog=DbName; User Id=UserName; Password=password;"
SQLconn.ConnectionString = ConnString
SQLconn.Open()
Using bulkCopy Asd SqlBulkCopy = New SqlBulkCopy(SQLConn)
bulkCopy.DestinationTableName = "TableToWriteToInSQLSERVER"
Try
objDR = objCmdSelect.ExecuteReader
bulCopy.WriteToServer(objDR)
objDR.Close()
SQLConn.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Using
If it's a one-off job, use DTS or SSIS. No code required.
Otherwise, you can open Excel as a data source, suck up its contents and insert into your database.