使用变量查询SQL。 [英] Query SQL using variables.

查看:52
本文介绍了使用变量查询SQL。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我为所有专家提出一个简单的(?)问题。

我有一个按钮,可以对我的SQL执行查询并填写数据网格

表格

代码:

Dim sconn As New SqlConnection()

sconn.ConnectionString =" Data Source =(my.Server; Initial

Catalog = MyDB; Persist Security Info = True; User ID = sa; Password = Mypass"

Dim filterresults As New SqlDataAdapter( 选择*来自

联系人,sconn

Dim contactsresult As New DataSet(result)

If Not IsNothing( filterresults)然后<​​br />
filterresults.Fill(contactsresult)

filterresults.Dispose()

结束如果

我。 ContactsDataGridView.DataSource = contactsresult.Tables(0)


代码有效,我得到了我的结果。


我的下一步将是来自我的代码的两个变量并在查询IE上使用它们

:(我从两个组合框中收集到哪个btw他一样

表格)

从联系人中选择* WHERE CITY = VARIABLE AND TYPE = VARIABLE"


我缺乏知识吗? ?任何人都可以帮助我吗?

代码示例将非常感激

解决方案



< R。***** @ gmail.com写信息

新闻:3e ************************ ********** @ t1g2000p ra.googlegroups.com ...



我有一个简单的(?)问题给你所有专家。

我有一个按钮,对我的SQL执行查询并填写数据网格

表格

代码:

Dim sconn As New SqlConnection()

sconn.ConnectionString =" Data Source =(my.Server; Initial

Catalog = MyDB; Persist Security Info = True; User ID = sa; Password = Mypass"

Dim filterresults As New SqlDataAdapter(" Select * from

contacts",sconn)

Dim contactsresult As New DataSet(result)

如果Not IsNothing(filterresults)那么

filterresults.Fill(contactsresult)

filterresults.Dispose()

结束如果

Me.ContactsDataGridView.DataSource = contactsresult.Tables(0)


代码有效,我得到了我的结果。


我的下一步是从我的代码中获取两个变量并在查询IE上使用它们

:(我从两个组合框中收集相同的

表格)

从联系人中选择* WHERE CITY = VARIABLE AND TYPE = VARIABLE"


我缺乏知识?有人可以帮我吗?

代码示例将非常感激

Dim var1 as string

Dim var2 as string

dim sqlstr as string


var1 =" hello"

var2 =" to me

sqlstr = 从联系人中选择* WHERE CITY =" & var1&" AND TYPE =" & var2


SqlDataAdapter(strsql,sconn)


如果您需要的数据是数字,那么就这样做。


dim var1 as int

dim var2 as int


var1 = 9

var2 = 10

sqlstr ="从联系人中选择* WHERE CITY CODE =" & var1.ToSrting()&" AND

TYPE =" & var2.ToString()


1月1日下午2:49,Mr。阿诺德" < MR。 Arn ... @ Arnold.comwrote:


< R.Ra ... @ gmail.comwrote in message


新闻:3e ********************************** @ t1g2000p ra.googlegroups.com ...



我有一个简单的(?)问题给你所有专家。

我有一个按钮,对我的SQL和填充执行查询一个数据网格

表格

代码:

Dim sconn As New SqlConnection()

sconn.ConnectionString =" Data Source =(my.Server; Initial

Catalog = MyDB; Persist Security Info = True; User ID = sa; Password = Mypass"

Dim filterresres As新的SqlDataAdapter(" Select * from

contacts",sconn)

Dim contactsresult As New DataSet(result)

If Not IsNothing(filterresults)然后<​​br />
filterresults.Fill(contactsresult)

filterresults.Dispose()

结束如果

Me.ContactsDataGridView.DataSource = contactsresult.Tables(0)


代码有效,我得到了我的结果。


我的下一步将是从我的代码中获取两个变量并在查询IE上使用它们

:(我从两个组合框中收集相同的

表格)

从联系人中选择* WHERE CITY = VARIABLE AND TYPE = VARIABLE"


我缺乏知识?有没有人可以帮助我?

代码示例将非常感激


Dim var1 as string

Dim var2 as string

dim sqlstr as string


var1 =" hello"

var2 =" to me


sqlstr ="从联系人中选择* WHERE CITY =" & var1&" AND TYPE =" & var2


SqlDataAdapter(strsql,sconn)


如果您需要的数据是数字,那么就这样做。


dim var1 as int

dim var2 as int


var1 = 9

var2 = 10


sqlstr ="从联系人中选择* WHERE CITY CODE =" & var1.ToSrting()&" AND

TYPE =" & var2.ToString()



您好Mr.arnold并感谢您的回复!

它给了我sqlstr上的syntex错误,它只是不会使用

变量......

和它的文字我查询的不是数字。


有什么建议吗?




< R。***** @ gmail.comwrote in message

新闻:71 ********************************** @ q77g2000 hsh.googlegroups.com ...

1月1日下午2:49,Mr。阿诺德" < MR。 Arn ... @ Arnold.comwrote:


< R.Ra ... @ gmail.comwrote in message


新闻:3e ********************************** @ t1g2000p ra.googlegroups.com ...



< snipped>

您好Mr.arnold并感谢您的回复!

它给了我一个syntex错误在sqlstr上,它只是不会使用

变量...

它的文字我查询的不是数字。


您也可以使用ToString()作为字符串数据。


有什么建议吗?

--------- -------

你需要做的是在构建strsql变量的
之后立即在线上加一个制动点。


然后,您需要使用Quick Watch,复制为SQL语句创建的字符串数据的文本。您可以右键单击该变量以获得快速监视




如果您的计算机上有SQL Server且它是SQL Server 2000,那么您

将启动SQL Server查询分析器并将SQL文本传递到窗格中,

突出显示并执行它。


或者如果你有SQL 2005,然后你使用SQL Server管理

Studio,新查询,并超过窗格中的文本并突出显示并执行

it。 br />

让SQL Server使用上面的

解决方案之一告诉你该语句有什么问题..


Hi,
I have a simple (?) question for you all experts.
I have a button that performs a query on my SQL and fill a datagrid on
the form
The code:
Dim sconn As New SqlConnection()
sconn.ConnectionString = "Data Source=(my.Server;Initial
Catalog=MyDB;Persist Security Info=True;User ID=sa;Password=Mypass"
Dim filterresults As New SqlDataAdapter("Select * from
contacts ", sconn)
Dim contactsresult As New DataSet("result")
If Not IsNothing(filterresults) Then
filterresults.Fill(contactsresult)
filterresults.Dispose()
End If
Me.ContactsDataGridView.DataSource = contactsresult.Tables(0)

The code works, and I get my results.

My next step would be taking two variables from my code and use them
on the query IE: (which btw I collect from two combo boxes on the same
form)
Select * from contacts WHERE CITY=VARIABLE AND TYPE=VARIABLE"

I lack the knowladge ?? could anyone help me out here?
Code sample would be GREATLY appreciated

解决方案


<R.*****@gmail.comwrote in message
news:3e**********************************@t1g2000p ra.googlegroups.com...
Hi,
I have a simple (?) question for you all experts.
I have a button that performs a query on my SQL and fill a datagrid on
the form
The code:
Dim sconn As New SqlConnection()
sconn.ConnectionString = "Data Source=(my.Server;Initial
Catalog=MyDB;Persist Security Info=True;User ID=sa;Password=Mypass"
Dim filterresults As New SqlDataAdapter("Select * from
contacts ", sconn)
Dim contactsresult As New DataSet("result")
If Not IsNothing(filterresults) Then
filterresults.Fill(contactsresult)
filterresults.Dispose()
End If
Me.ContactsDataGridView.DataSource = contactsresult.Tables(0)

The code works, and I get my results.

My next step would be taking two variables from my code and use them
on the query IE: (which btw I collect from two combo boxes on the same
form)
Select * from contacts WHERE CITY=VARIABLE AND TYPE=VARIABLE"

I lack the knowladge ?? could anyone help me out here?
Code sample would be GREATLY appreciated
Dim var1 as string
Dim var2 as string
dim sqlstr as string

var1 = "hello"
var2 = "to me"
sqlstr = "Select * from contacts WHERE CITY = " &var1 &" AND TYPE = " &var2

SqlDataAdapter(strsql, sconn)

if the data you needed was numeric, then you do this.

dim var1 as int
dim var2 as int

var1 = 9
var2 = 10
sqlstr = "Select * from contacts WHERE CITY CODE = " &var1.ToSrting() &" AND
TYPE = " &var2.ToString()


On Jan 1, 2:49 pm, "Mr. Arnold" <MR. Arn...@Arnold.comwrote:

<R.Ra...@gmail.comwrote in message

news:3e**********************************@t1g2000p ra.googlegroups.com...
Hi,
I have a simple (?) question for you all experts.
I have a button that performs a query on my SQL and fill a datagrid on
the form
The code:
Dim sconn As New SqlConnection()
sconn.ConnectionString = "Data Source=(my.Server;Initial
Catalog=MyDB;Persist Security Info=True;User ID=sa;Password=Mypass"
Dim filterresults As New SqlDataAdapter("Select * from
contacts ", sconn)
Dim contactsresult As New DataSet("result")
If Not IsNothing(filterresults) Then
filterresults.Fill(contactsresult)
filterresults.Dispose()
End If
Me.ContactsDataGridView.DataSource = contactsresult.Tables(0)

The code works, and I get my results.

My next step would be taking two variables from my code and use them
on the query IE: (which btw I collect from two combo boxes on the same
form)
Select * from contacts WHERE CITY=VARIABLE AND TYPE=VARIABLE"

I lack the knowladge ?? could anyone help me out here?
Code sample would be GREATLY appreciated

Dim var1 as string
Dim var2 as string
dim sqlstr as string

var1 = "hello"
var2 = "to me"

sqlstr = "Select * from contacts WHERE CITY = " &var1 &" AND TYPE = " &var2

SqlDataAdapter(strsql, sconn)

if the data you needed was numeric, then you do this.

dim var1 as int
dim var2 as int

var1 = 9
var2 = 10

sqlstr = "Select * from contacts WHERE CITY CODE = " &var1.ToSrting() &" AND
TYPE = " &var2.ToString()

Hi Mr.arnold and thanks for the reply!
it gives me a syntex error on the sqlstr, it''s just wont use the
vars...
and it''s text i query not numeric.

any suggestions?



<R.*****@gmail.comwrote in message
news:71**********************************@q77g2000 hsh.googlegroups.com...
On Jan 1, 2:49 pm, "Mr. Arnold" <MR. Arn...@Arnold.comwrote:

<R.Ra...@gmail.comwrote in message

news:3e**********************************@t1g2000p ra.googlegroups.com...

<snipped>
Hi Mr.arnold and thanks for the reply!
it gives me a syntex error on the sqlstr, it''s just wont use the
vars...
and it''s text i query not numeric.

You can use the ToString() for the string data too.

any suggestions?
----------------
What you need to do is put a brake point on the line right after the build
of the strsql variable.

You then need to use the Quick Watch, copy the text of the string data that
was created for the SQL statement. You can right-click the variable to get
to Quick Watch.

If you have SQL Server on the machine and it is SQL Server 2000, then you
will start SQL Server Query Analyzer and past the SQL text into the pane,
highlight and execute it.

Or if it is SQL 2005 that you have, then you use the SQL Server Management
Studio, New Query, and past the text in the pane and highlight and execute
it.

Let SQL Server tell you what''s wrong with the statement by using one of the
solutions above..


这篇关于使用变量查询SQL。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆