| 
 
查看: 1251|回复: 2
 
 | 
请问要怎么把listbox里面的名字存入database?
[复制链接]
 | 
 
 
 | 
 | 
 
我用两个listbox1和2 
现在我从database拿出Empname放进listbox1 
现在listbox1有十个名字了 
当我选着一个名时 
这个名字就会跳过去listbox2 
现在在listbox2有三个名字了 
如果我要click save的时候 
只有这三个名字的ID和Name会给出入database 
要怎么写?? 
 
下面的code是我试写的 
可是不懂那里错... 
资料存不进去... 
 
-         Dim myCommand As New OdbcCommand
 
 -         Dim myData As New DataSet
 
 -         Dim SQL As String
 
 -         Dim myAdapter As OdbcDataAdapter
 
 -         Dim conn As OdbcConnection
 
 -         Dim acc As Data.DataRow
 
 -         Dim a As OdbcCommandBuilder
 
  
 
-         conn = New OdbcConnection
 
 -         conn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" & _
 
 -                        "SERVER=localhost;" & _
 
 -                        "DATABASE=seas;" & _
 
 -                        "UID=root;" & _
 
 -                        "PASSWORD=;" & _
 
 -                        "OPTION=3;"
 
  
-         conn.Open()
 
 -         For i = 0 To ListBox2.Items.Count - 1
 
  
-             SQL = "select Empname from timetable3"
 
 -             myAdapter = New OdbcDataAdapter(SQL, conn)
 
 -             a = New OdbcCommandBuilder(myAdapter)
 
 -             myAdapter.Fill(myData, "timetable3")
 
 -             acc = myData.Tables("timetable3").NewRow
 
 -             acc("Empname") = ListBox2.GetItemText(i)
 
  
 
-             myData.Tables("timetable3").Rows.Add(acc)
 
 -             myAdapter.Update(myData, "timetable3")
 
  
-         Next
 
 -         conn.Close()
 
 -         MsgBox("Update Success!", MsgBoxStyle.Information)
 
 
  复制代码 |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
发表于 15-7-2005 12:47 AM
|
显示全部楼层
 
 
 
你 for loop 里的 code 太复杂了. 
不需要那么麻烦, 把 adapter, commandbuilder 全部删除, 
 
你只需要一个 OdbcConnection, OdbcCommand, 例子: 
 
    Dim myConnection As New OdbcConnection(myConnectionString) 
    Dim myCommand As New OdbcCommand("INSERT INTO...", myConnection) 
    myCommand.Connection.Open() 
    myCommand.ExecuteNonQuery() 
    MyConnection.Close() |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 | 
 | 
 
 
 楼主 |
发表于 17-7-2005 02:29 AM
|
显示全部楼层
 
 
 
呵呵~~~ 
谢谢了~ 
问题解决 
  |   
 
 
 
 |  
| 
 | 
 
 
 | 
 | 
 
| 
 | 
 | 
 
 
 
 
 |   | 
            本周最热论坛帖子
 
 
 
 |