I need a SQL query in c# windows + sql server app to check whether the ip address of the user is in database(a kind of black list) or not?

StackOverflow https://stackoverflow.com/questions/23142311

  •  05-07-2023
  •  | 
  •  

Question

I am designing a c# application in which client sends register request to server. Server checks black list (table in database consisting of ip addresses of blocked users) to find if the ip address of client is present in it or not.

If ip address is in black list it will reject the register request else will entertain the request.

I have a string named ip in which ip address of current client is stored.

I need a SQL query to find whether the ip is in black list or not.

Here is my code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Net;
using System.Data.SqlClient;

    namespace sip_server
    {
        public partial class Form1 : Form
        {
            Socket sck;
            // EndPoint epLocal, epRemote;
            IPAddress ipAdress;
            TcpListener myList;
            string serverIP;


            public Form1()
            {
                InitializeComponent();

                //serverIP = GetLocalIP();
                serverIP = "127.0.0.1";



            }
            private string GetLocalIP()
            {
                IPHostEntry host;
                host = Dns.GetHostEntry(Dns.GetHostName());
                foreach (IPAddress ip in host.AddressList)
                {
                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                    {
                        return ip.ToString();
                    }
                }
                return "127.0.0.1";
            }
            private void Form1_Load(object sender, EventArgs e)
            {

            }

            private void btnStart_Click(object sender, EventArgs e)
            {
                btnStart.Enabled = false;
                ipAdress = IPAddress.Parse(serverIP);
                myList = new TcpListener(ipAdress, Convert.ToInt32("8000"));
                myList.Start();
                sck = myList.AcceptSocket();
                btnStart.Text = "Connected";
                lblCAFrom.Text = "Connection accepted from";
                txtCIP.Text = sck.RemoteEndPoint.ToString();

                string input = "";
                sck.Send(Encoding.ASCII.GetBytes(input));
                byte[] data = new byte[1024];
                int receivedDataLength = sck.Receive(data);
                string stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength);
                string[] arr = stringData.Split(new char[] { ';' }, 2);
                lblReceived.Text = "Data Received:";
                lblPubID.Visible = true;
                lblPrivateID.Visible = true;
                txtPublicID.Visible = true;
                txtPrivateID.Visible = true;
                txtPublicID.Text = arr[0];
                txtPrivateID.Text = arr[1];
                txtCIP.Visible = true;



            }




          private void txtCIP_TextChanged(object sender, EventArgs e)
            {

            }
            private void txtLocalPort_TextChanged(object sender, EventArgs e)
            {

            }

            private void lblReceived_Click(object sender, EventArgs e)
            {

            }

            **private void btnChBL_Click(object sender, EventArgs e)
            {
                string conString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Ayne\Documents\db.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
                SqlConnection con = new SqlConnection(conString);
                try
                {
                    btnChBL.Enabled = false;
                    con.Open();
                    MessageBox.Show("Connection Open ! ");
                    string[] ipPort = txtCIP.Text.Split(new char[] { ':' }, 2);
                    string ip = ipPort[0];
                    //string query = "SELECT IP from BlackList WHERE IP=ip";

                    //SqlCommand cmd = new SqlCommand(query, con);
                    //cmd.ExecuteNonQuery();
                    con.Close();
                    MessageBox.Show("This IP is black Listed.Retry after few seconds.");

                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.ToString());
                }**

            }
        }
    }
Was it helpful?

Solution

string query = "SELECT count(*) from BlackList WHERE IP=ip";

SqlCommand cmd = new SqlCommand(query, con);
var rowCount = (int)cmd.ExecuteScalar();
con.Close();
if (rowCount>0)
  MessageBox.Show("This IP is black Listed.Retry after few seconds.");

Notice the select count(*). This will return the number of rows that match your criteria. The the ExecuteScalar will return you the result of the query. You can safely cast this to an int as you are expecting a count back from your query.

Once this is done if the rowCount is greater than 0 you have a blacklisted ip.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top