我想这样做一个查询。
SELECT @rownum:=@rownum+1 'rank' FROM (SELECT @rownum:=0) r
当我在我的mysql查询浏览器上测试时,它运行良好。
现在,我想用VB在mysqlCommand中执行它。当然@rownum
被定义为参数并等待其值。如何在VB中执行该查询?
答案 0 :(得分:1)
您需要将变量添加到命令的参数中:
command.Parameters.AddWithValue("@rownum", rownum)
有些文档在这里:http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqlparametercollection.html
答案 1 :(得分:1)
您必须将此密钥添加到您的连接字符串中:'允许用户变量= True;'
答案 2 :(得分:0)
dim conn As New MySqlConnection <br>
dim comm As New MySqlCommand<br><br>
dim dAdapter As New MySqlDataAdapter<br>
dim ddata As New DataSet<br>
conn.ConnectionString = "DATABASE=" & ddatabase & ";SERVER=" & Trim(TextBox1.Text) & ";user id=dev;password=revolution;port=" & TextBox3.Text & " ;charset=utf8;<b>Allow User Variables=True" </b><br>
conn.Open() <br>
<br>
comm.Connection = conn<br>
comm.CommandText = "set @rownum = 10"<br>
comm.ExecuteNonQuery() <br>
<br>
ddata.Clear()<br>
ddata.Tables.Clear()<br>
comm.Connection = conn<br>
comm.CommandText = "select @rownum rownum"<br>
dAdapter.SelectCommand = comm <br>
dAdapter.Fill(ddata)<br>
<br>
msgbox(ddata.Tables(0).Rows(0).Item("rownum")) <br>