Προς το περιεχόμενο

Προτεινόμενες αναρτήσεις

Δημοσ.

Έχω ένα θέμα με την Visual Basic 2010

Θέλω να προσθέτω service σε εναν πίνακα στην MySQL

 

 

Αλλά κάπου έχω κάνει λάθος στον κώδικα , υπάρχει

κανένα καλό παιδάκι που να μπορεί να με βοηθήσει ?

 

Ο κώδικας είναι αυτός

ds = New DataSet
da = New MySqlDataAdapter("insert into service (sid,category,scategory,cost,tech,desc,paymethod) values ('" & sid.Text & "','" & category.Text & "','" & scategory.Text & "','" & adate.Text & "','" & pdate.Text & "','" & cost.Text & "','" & tech.Text & "','" & desc.Text & "','" & paymethod.Text & "')", mycon)
da.Fill(ds, "service")
 

Μου βγάζει το εξής λάθος

 

MySql.Data.MySqlClient.MySqlException was unhandled
  ErrorCode=-2147467259
  HResult=-2147467259
  Message=You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc,paymethod) values ('71607','Internet','','Πέμπτη, 12 Δεκεμβρί' at line 1
  Number=1064
  Source=MySql.Data
  StackTrace:
       σε MySql.Data.MySqlClient.MySqlStream.ReadPacket()
       σε MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
       σε MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId)
       σε MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
       σε MySql.Data.MySqlClient.MySqlDataReader.NextResult()
       σε MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
       σε MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
       σε System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
       σε System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
       σε System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
       σε System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)
       σε technote.service.Insert_service_Click(Object sender, EventArgs e) στο J:\technote13\technote11\technote\technote\service.vb:γραμμή 102
       σε System.Windows.Forms.Control.onclick(EventArgs e)
       σε System.Windows.Forms.Button.onclick(EventArgs e)
       σε System.Windows.Forms.Button.onmouseup(MouseEventArgs mevent)
       σε System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       σε System.Windows.Forms.Control.WndProc(Message& m)
       σε System.Windows.Forms.ButtonBase.WndProc(Message& m)
       σε System.Windows.Forms.Button.WndProc(Message& m)
       σε System.Windows.Forms.Control.ControlNativewindow.OnMessage(Message& m)
       σε System.Windows.Forms.Control.ControlNativewindow.WndProc(Message& m)
       σε System.Windows.Forms.Nativewindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       σε System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       σε System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       σε System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       σε System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       σε System.Windows.Forms.Application.Run(ApplicationContext context)
       σε Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       σε Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       σε Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       σε technote.My.MyApplication.Main(String[] Args) στο 17d14f5c-a337-4978-8281-53493378c1071.vb:γραμμή 81
       σε System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       σε System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       σε Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       σε System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       σε System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       σε System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       σε System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       σε System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
 
Δημοσ.

Το πρόβλημά σου είναι στη sql, συγκεκριμένα στο όνομα του πεδίου desc. Αυτή είναι μια reserved keyword στη sql και για να τη χρησιμοποιήσεις σαν ονόματα στο schema πρέπει να τα βάλεις μέσα σε backticks:

`desc`

Θα ήταν καλά να ονομάσεις πιο σωστά το πεδίο description, στην οποία περίπτωση αυτό το πρόβλημα δε θα υπήρχε εξαρχής.

  • Like 1
Δημοσ.

Επίσης παραμέτρους δεν είναι σωστή πρακτική η συνένωση string για sql εντολή.

 

http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqldataadapter.html

 

Public Sub CreateSqlDataAdapter()
Dim conn As MySqlConnection = New MySqlConnection("Data Source=localhost;" & _
"database=test")
Dim cmd as new MySqlCommand("SELECT id, name FROM mytable", conn)
Dim da As MySqlDataAdapter = New MySqlDataAdapter(cmd)
da.MissingSchemaAction = MissingSchemaAction.AddWithKey

da.InsertCommand = New MySqlCommand("INSERT INTO mytable (id, name) " & _
"VALUES (?id, ?name)", conn)

da.UpdateCommand = New MySqlCommand("UPDATE mytable SET id=?id, name=?name " & _
"WHERE id=?oldId", conn)
da.DeleteCommand = New MySqlCommand("DELETE FROM mytable WHERE id=?id", conn)
da.InsertCommand.Parameters.Add("?id", MySqlDbType.VarChar, 5, "id")
da.InsertCommand.Parameters.Add("?name", MySqlDbType.VarChar, 40, "name")

da.UpdateCommand.Parameters.Add("?id", MySqlDbType.VarChar, 5, "id")
da.UpdateCommand.Parameters.Add("?name", MySqlDbType.VarChar, 40, "name")
da.UpdateCommand.Parameters.Add("?oldId", MySqlDbType.VarChar, 5, "id").SourceVersion = DataRowVersion.Original
da.DeleteCommand.Parameters.Add("?id", MySqlDbType.VarChar, 5, "id").SourceVersion = DataRowVersion.Original
End Sub

Επίσης MySqlCommand για Insert,Update,Delete.MySqlDataAdapter χρησιμοποιείται για Select,όταν θες να πάρεις δεδομένα από την βάση.

  • Like 1
Δημοσ.

Καλημέρα. defacer το έκανα αυτό που μου είπες τώρα όμως κολλάει σε άλλο σημείο και μου βγάζει αυτό το error : Column count doesn't match value count at row 1. Για να σιγουρευτώ πως δεν έχω κάνει κάποιο λάθος στην βάση την ξανά έφτιαξα από την αρχή. Nucleus το συγκεκριμένο string που έχω postαρει το χρησιμοποιώ και σε άλλες φόρμες και δουλεύει σωστά...Θα κοιτάξω όμως αυτό postαρες...

Δημοσ.

Καλημέρα. defacer το έκανα αυτό που μου είπες τώρα όμως κολλάει σε άλλο σημείο και μου βγάζει αυτό το error : Column count doesn't match value count at row 1.

Απλά αγγλικά: αναφέρεις Χ ονόματα στηλών και δίνεις Υ τιμές για να μπουν σ' αυτές τις στήλες. Χ != Υ.

Δημοσ.

Του λές να κάνει INSERT (x, y, z) VALUES (1, 2). Τα πεδία είναι 3 και οι τιμές 2 (ή κάτι αντίστοιχο). Κάτσε και μέτρα τα και δες πού έκανες λάθος.

Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε

Πρέπει να είστε μέλος για να αφήσετε σχόλιο

Δημιουργία λογαριασμού

Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!

Δημιουργία νέου λογαριασμού

Σύνδεση

Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.

Συνδεθείτε τώρα
  • Δημιουργία νέου...