SQL Query to get 1 value to a C# application for forwarding to the PLC

userxyz

Member
Join Date
May 2002
Location
any
Posts
2,768
Hi,

I have a question about SQL.
This question is a little to much on the IT side I suppose, but I will give it a try anyway as I do not have an IT forum account :).

I'm writing a user management application with libnodave onboard and SQL. The customer wants an application that manages the user management. With an rfid reader, a UID will be red and forwarded from the HMI to the PLC and to the application on the server. I must use that UID to query in a SQL database and get the protection level.

The connection to the SQL database works. Using a SQLNonQuery for storing values works as well. But I've never queried data from a C# application, so I'm in a testing phase for this. I try with the ExecuteScalar command because I only need 1 value.

The folowing code does not work:
Code:
public static void SQLGetUser(String Server, String Database)
            {
            string connetionString = null;
            SqlConnection cnn;
            connetionString = "Server= " + Server + "  ; Database=" + Database + ";Integrated Security = SSPI";
            cnn = new SqlConnection(connetionString);

            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = System.Data.CommandType.Text;

            string cmdString = "Select * FROM Table_UsrMngt";

            cmd.CommandText = cmdString;
            cmd.Connection = cnn;

            try
            {
                cnn.Open();
                int Result = (int)cmd.ExecuteScalar();
                MessageBox.Show(Convert.ToString(Result));

            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not open connection ! " + ex);
            }

            cnn.Close();
            }

Are there SQL geeks out there to see where I go wrong ?

Thanks in advance,

Kind regards,
G
 
Last edited:
Found :)

int Result = (int)cmd.ExecuteScalar();

Had to be string instead of int, because my first column is a User in the table.
Works fine now :)
 
But now

But the where doesn't function...


Code:
       public static void SQLGetUserRights(String Server, String Database, String Table, String UID)
            {
            string connetionString = null;
            SqlConnection cnn;
            connetionString = "Server= " + Server + "  ; Database=" + Database + ";Integrated Security = SSPI";
            cnn = new SqlConnection(connetionString);

            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = System.Data.CommandType.Text;
            string cmdString = "Select UserRights FROM " + Table; // + " WHERE UserName = " + UID;
            MessageBox.Show(cmdString);
            cmd.CommandText = cmdString;
            cmd.Connection = cnn;
            try
            {
                cnn.Open();
                int Result = (int)cmd.ExecuteScalar();
                MessageBox.Show(Convert.ToString(Result));
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not open connection ! " + ex);
            }
            cnn.Close();
            }

When I activate this line: // + " WHERE UserName = " + UID;
The query is then:

Select UserRights FROM Table_UsrMngt WHERE UserName = 'John'

This doesn't work for some reason... I get the message that the column John doesn't exsist ?
 
...

In SQL SSMS directly, the Query works...
I hope it is not because Execute Scalar can't handle this... ?
 
Found !!
The query in the C# code misses the '' around the name John:
Select UserRights FROM Table_UsrMngt WHERE UserName = 'John'

It has to be: string cmdString = "Select UserRights FROM " + Table + " WHERE UserName = " + "'" + UID + "'";
 

Similar Topics

Good Evening, I'm loading a recipe into a Rockwell ControlLogix or CompactLogix CPU via Excel, this is all working fine. Is there anyway I can...
Replies
7
Views
2,043
Hi there, I use Indusoft IWS8.0 & I have created a sample Production data Date&time, MachineNo,Production Data with SQL Server 2008 DB. Need to...
Replies
1
Views
1,877
Hello, For my general information and knowledge, how is querying and storing data from a AB PLC (Compact or Control Logix) typically...
Replies
15
Views
7,372
Goodevening all... I have a Redlion PTV and Graphite with built in webserver... I can create the simple default.htm page... drag in tags to...
Replies
1
Views
1,907
Hi all, Having a little trouble getting the result I require, I'm close but no cigar. Basically, I am wanting to display a table within the...
Replies
2
Views
3,929
Back
Top Bottom