Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys ,

I make small program to text box to added in sql but with connect sql IP,Username,Password and that connect work correctly as I know but I tried a lot of code and I can`t put value my table have only one colum name is ip ... botnet name is database , table name is test , colum name is ip
C#
string connetionString;
SqlConnection cnn;
connetionString = @"Data Source=192.168.1.21,1433;Initial Catalog=botnet;User ID=sa;Password=***************";
cnn = new SqlConnection(connetionString);
cnn.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO test VALUES @ip", cnn);
cmd.Parameters.AddWithValue("@ip", textBox1.Text);
MessageBox.Show("Check SQL Database");


What I have tried:

i tried
C#
SqlCommand cmd = new SqlCommand("INSERT INTO (test) VALUES (@ip)", cnn);
SqlCommand cmd = new SqlCommand("INSERT INTO botnet VALUES @ip", cnn);
Posted
Updated 27-Jul-20 13:29pm
v3
Comments
Dave Kreskowiak 27-Jul-20 17:36pm    
NEVER use the SA account when connecting to the database. Use an account dedicated to the application with as limited permissions as possible for the app to do what it needs to do with the database.

And, next time you post a question, you might want to remove any passwords from what you paste. If you're used this passwords anywhere else, I would suggest you go through your accounts and changes passwords. Now.
Ahmed Adel 27-Jul-20 18:03pm    
i don`t think so about account , because someone make it with java and used sa account without problem , and thanks for help about password too that password only for test
Dave Kreskowiak 27-Jul-20 19:27pm    
Well, you would be wrong. You're giving the application FULL ADMIN PERMISSIONS to the ENTIRE DATABASE SERVER!!

Write your code badly and you've given the application the permissions it needs to destroy the entire database server and all of the data on it.

It's not a problem for the app as it will have all of the permissions it needs to do whatever job is required of it, but if you did this in a production environment at a company that had even the smallest clue, you would be fired on the spot.

Not only that, but you posted the SA account password ON A PUBLIC FORUM THE ENTIRE WORLD CAN SEE!!! I'D FIRE YOU RIGHT NOW IF I WAS YOUR MANAGER.

You never execute the command. Also, you should be using using statements for proper memory disposal. Something like this:

C#
using (SqlConnection sqlCon = new SqlConnection(connectionString){
  sqlCon.Open();
  using (SqlCommand cmd = new SqlCommand ("INSERT INTO test VALUES (@ip)", sqlConn){
    cmd.Parameters.AddWithValue("@ip", textBox1.Text);
    cmd.ExecuteNonQuery();  // this is the line that actually sends it to the database.
  }
}
 
Share this answer
 
Comments
Member 15627531 7-May-22 3:08am    
Thank you for this solution, it really helps.
i tried this code and not help
 
Share this answer
 
Comments
Dave Kreskowiak 27-Jul-20 20:11pm    
You posted this as an answer to your own question. If you want to reply to someone, click the "Have a Question or Comment" button below the post you're replying to. That way, that person gets a notification that you replied.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900