Click here to Skip to main content
15,898,374 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Dim ddi As String = Date.Today
       Dim Mycon As String = ("C:\Users\Bruno Soares\Documents\Visual Studio 2012\Projects\Projecto Castramento\Projecto Ca\Database1.sdf")
       Dim csvconnectionstring As String = My.Computer.FileSystem.CurrentDirectory & "Database1.sdf"

       Using cn As New SqlConnection(Mycon)
           cn.Open()

           Using cmd As New SqlCommand("INSERT INTO Cliente(Nome, Endereço, Complemento, Número, Bairro, CEP, DDi, DDn, Sexo, Telefone, Celular, Email, Visitas) VALUES('" & nome.Text & "','" & endereço.Text & "','" & complemento.Text & "','" & numero.Text & "','" & bairro.Text & "', #" & cep.Text & "#, " & ddi & "," & niver.Text & "," & sexo.SelectedItem & telefone.Text & celular.Text & email.Text & 0 & ")", cn)

               Try
                   With cmd
                       .Connection = cn
                       .CommandType = CommandType.StoredProcedure
                       .Parameters.Add("@Nome", SqlDbType.Int, 4)
                       .Parameters("@Nome").Value = nome.Text
                   End With


               Catch ex As Exception

               End Try
               cn.Close()
           End Using
       End Using


System.ArgumentException was unhandled
  HResult=-2147024809
  Message=O formato da cadeia de inicialização não está de acordo com a especificação iniciada no índice 0.
  Source=System.Data
  StackTrace:
       em System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue)
       em System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey)
       em System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules)
       em System.Data.SqlClient.SqlConnectionString..ctor(String connectionString)
       em System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous)
       em System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions)
       em System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key)
       em System.Data.SqlClient.SqlConnection.set_ConnectionString(String value)
       em System.Data.SqlClient.SqlConnection..ctor(String connectionString, SqlCredential credential)
       em System.Data.SqlClient.SqlConnection..ctor(String connectionString)
       em Projecto_cadastro_de_gente_fina.Cadastro.Button1_Click(Object sender, EventArgs e) na C:\Users\Bruno Soares\Documents\Visual Studio 2012\Projects\Projecto Castramento\Projecto Castramento\Cadastro.vb:linha 11
       em System.Windows.Forms.Control.OnClick(EventArgs e)
       em System.Windows.Forms.Button.OnClick(EventArgs e)
       em System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       em System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       em System.Windows.Forms.Control.WndProc(Message& m)
       em System.Windows.Forms.ButtonBase.WndProc(Message& m)
       em System.Windows.Forms.Button.WndProc(Message& m)
       em System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       em System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       em System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       em System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       em System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       em System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       em System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       em System.Windows.Forms.Application.Run(ApplicationContext context)
       em Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       em Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       em Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       em Projecto_cadastro_de_gente_fina.My.MyApplication.Main(String[] Args) na 17d14f5c-a337-4978-8281-53493378c1071.vb:linha 81
       em System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       em System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       em System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       em Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       em System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       em System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       em System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       em System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       em System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Hello!
The code outlined above is causing an exception Handled
Posted
Comments
[no name] 13-Jun-13 10:51am    
http://www.connectionstrings.com/sql-server-ce

1 solution

How can I Insert data into SQL Server using VBNet[^]

Update :

Ok, a .sdf file is a SQL Compact database, and you will need to use the 'SqlCeConnection' class to connect to.

Example from MSDN :
VB
Dim conn As SqlCeConnection = Nothing

Try
    conn = New SqlCeConnection("Data Source = MyDatabase.sdf; Password ='<pwd>'")
    conn.Open()

    Dim cmd As SqlCeCommand = conn.CreateCommand()
    cmd.CommandText = "INSERT INTO Customers ([Customer ID], [Company Name]) Values('NWIND', 'Northwind Traders')"

    cmd.ExecuteNonQuery()
Finally
    conn.Close()
End Try
 
Share this answer
 
v3
Comments
Bruno C Soares 13-Jun-13 10:37am    
my problem is how to establish a connection to a local database
Ahmed Bensaid 13-Jun-13 10:52am    
I updated my answer ;)
Bruno C Soares 13-Jun-13 11:06am    
is a file. sdf which is on my computer.
I tried numerous ways and they all give error connection
Ahmed Bensaid 13-Jun-13 11:18am    
Updated again ;)
Bruno C Soares 13-Jun-13 11:25am    
What if I put file contains no password?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900