Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3314140
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:11:48+00:00 2026-05-17T22:11:48+00:00

I have this code UPDATE OPENQUERY (db,’SELECT * FROM table WHERE ref = ”+

  • 0

I have this code

UPDATE OPENQUERY (db,'SELECT * FROM table WHERE ref = ''"+ Ref +"'' AND bookno = ''"+ Session("number") +"'' ') 

How would I prevent SQL Injections on this?

UPDATE

Here’s what i’m trying

SqlCommand cmd = new SqlCommand("Select * from Table where ref=@ref", con); 
cmd.Parameters.AddWithValue("@ref", 34);

For some reason everything I try and add it doesn’t seem to work I keep getting SQL Command mentioned below.

The error is this

'SqlCommand' is a type and cannot be used as an expression

I’m taking over someone else’s work so this is all new to me and I would like do things the right way so if anyone can provide any more help on how to make my query above safe from SQL injections then please do.

UPDATE NO 2

I added in the code as VasilP said like this

Dim dbQuery As [String] = "SELECT * FROM table WHERE ref = '" & Tools.SQLSafeString(Ref) & "' AND bookno = '" & Tools.SQLSafeString(Session("number")) & "'"

But I get an error Tools is not declared do I need to specify a certain namespace for it to work?

UPDATE

Has anyone got any ideas on the best of getting my query safe from SQL injection without the errors that i’m experiencing?

UPDATE

I now have it so it work without the parameters bit here’s my updated source code any idea why it won’t add the parameter value?

Dim conn As SqlConnection = New SqlConnection("server='server1'; user id='w'; password='w'; database='w'; pooling='false'")
   conn.Open()


Dim query As New SqlCommand("Select * from openquery (db, 'Select * from table where investor = @investor ') ", conn)
query.Parameters.AddWithValue("@investor", 69836)

dgBookings.DataSource = query.ExecuteReader
dgBookings.DataBind()

It works like this

Dim conn As SqlConnection = New SqlConnection("server='server1'; user id='w'; password='w'; database='w'; pooling='false'")
   conn.Open()


Dim query As New SqlCommand("Select * from openquery (db, 'Select * from table where investor = 69836') ", conn)

dgBookings.DataSource = query.ExecuteReader
dgBookings.DataBind()

The error i’m getting is this

An error occurred while preparing a query for execution against OLE DB provider 'MSDASQL'. 

And it’s because it isn’t replacing the @investor with the 69836

Any ideas?

SOLUTION

Here is how I solved my problem

Dim conn As SqlConnection = New SqlConnection("server='h'; user id='w'; password='w'; database='w'; pooling='false'")

conn.Open()

Dim query As New SqlCommand("DECLARE @investor varchar(10), @sql varchar(1000) Select @investor = 69836 select @sql = 'SELECT * FROM OPENQUERY(db,''SELECT * FROM table WHERE investor = ''''' + @investor + ''''''')' EXEC(@sql)", conn)

dgBookings.DataSource = query.ExecuteReader
dgBookings.DataBind()

Now I can write queries without the worry of SQL injection

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:11:49+00:00Added an answer on May 17, 2026 at 10:11 pm

    Try using a parameterized query here is a link http://www.aspnet101.com/2007/03/parameterized-queries-in-asp-net/

    Also, do not use OpenQuery… use the this to run the select

    SELECT * FROM db...table WHERE ref = @ref AND bookno = @bookno
    

    More articles describing some of your options:

    http://support.microsoft.com/kb/314520

    What is the T-SQL syntax to connect to another SQL Server?


    Edited

    Note: Your original question was asking about distributed queries and Linked servers. This new statement does not reference a distributed query. I can only assume you are directly connecting to the database now. Here is an example that should work.
    Here is another reference site for using SqlCommand.Parameters

    SqlCommand cmd = new SqlCommand("Select * from Table where ref=@ref", con); 
    cmd.Parameters.Add("@ref", SqlDbType.Int);
    cmd.Parameters["@ref"] = 34;
    

    Edited:

    Ok Jamie taylor I will try to answer your question again.

    You are using OpenQuery becuase you are probably using a linked DB

    Basically the problem is the OpenQuery Method takes a string you cannot pass a variable as part of the string you sent to OpenQuery.

    You can format your query like this instead. The notation follows servername.databasename.schemaname.tablename. If you are using a linked server via odbc then omit databasename and schemaname, as illustrated below

        Dim conn As SqlConnection = New SqlConnection("your SQL Connection String")
        Dim cmd As SqlCommand = conn.CreateCommand()
        cmd.CommandText = "Select * db...table where investor = @investor"
        Dim parameter As SqlParameter = cmd.CreateParameter()
        parameter.DbType = SqlDbType.Int
        parameter.ParameterName = "@investor"
        parameter.Direction = ParameterDirection.Input
        parameter.Value = 34
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: void BaseOBJ::update(BaseOBJ* surround[3][3]) { forces[0]->Apply(); //in place of for loop
I have this code in javascript to show time in div and update it:
I have this code from win32com.client import Dispatch connection_string = Provider=SQLNCLI;server=%s;initial catalog=%s;user id=%s;password=%s%(server,db_name,user,pwd) dbConn
I have this code to set the culture public static CultureInfo GetRegionDefaultCulture(int regionId) {
I have this ajax jquery code: var form = document.getElementById('frm'); var data_string = form.serialize();
I have this code to get the caller of my function's file name, line
I've got this field in my database year_start_1 and it is an integer field
Rails 2.3.5 I have a Folder model and a Contact Model. In a view,
function updateimage(){ $(#fileimg).attr(src,image.jpg); $('#fileimg').fadeIn('slow'); } function updatefiles(){ $.get('files.php', function(data) { $('#files').html(data); $('#files').fadeIn('slow'); }); }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.