Hello everybody,
How i can ask , if the line in my database table exist?
Like this C# example:
if ..... exist{
//nothing
}else{
//blablabla
}
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Shivanand ArurPosted Sep 29, 2012, 1:53 PM
command.CommandText = "Select 1 from test where ID = '" + trade.OtherSID + "'";
Reader = command.ExecuteReader();
command.CommandText = "INSERT INTO test (id) Values ('"+trade.OtherSID+"')";
Reader.Close();
Dharmesh SharmaPosted Oct 3, 2012, 4:27 PM
just do
IF(NOT EXISTS(select * from table))
BEGIN
DO SOMETING
END
ELSE
BEGIN
//DO SOMETING
END
Shivanand ArurPosted Sep 29, 2012, 2:00 PM
Shivanand ArurPosted Sep 29, 2012, 1:56 PM
Thank you.
meinenrPosted Sep 29, 2012, 1:54 PM
THANK YOU A LOT!
meinenrPosted Sep 29, 2012, 1:47 PM
Shivanand ArurPosted Sep 29, 2012, 1:45 PM
It is giving the error because I have not written proper statement. So you try writing your own Select and Insert Script with the same logic I have applied and check it....
Best thing is, that you first write and test the scripts in the MySql Server first... If it works fine, then copy and paste it in your code...
meinenrPosted Sep 29, 2012, 1:40 PM
Errors:
command.CommandText = "Select 1 from test where ID = '" + trade.OtherSID + "'"";
Reader = command.ExecuteReader();
-> unexepted Symbol ';'
command.CommandText = "Insert into test(id) Values('" + trade.OtherSID + "'" )";
Reader.Close();
-> unexepted Symbol ')' exepting ';' or '}'
Shivanand ArurPosted Sep 29, 2012, 1:36 PM
using System;
using MySql.Data;
using MySql.Data.MySqlClient;
PLEASE MARK THE ANSWER AS ACCEPTED IF MY ANSWERS HAVE HELPED YOU.
Thank you.
meinenrPosted Sep 29, 2012, 1:31 PM
But I think i have to write using ... on the top?
What kind of using... to i need?
Do you have TeamViewer?
Than you can edit it, how you want and you can see the errors...
Shivanand ArurPosted Sep 29, 2012, 1:27 PM
meinenrPosted Sep 29, 2012, 1:07 PM
Shivanand ArurPosted Sep 29, 2012, 1:02 PM
meinenrPosted Sep 29, 2012, 12:33 PM
Can you please write the little part for me:
if the value of the trade.OtherSID already exists you can write:
//nothing happens
or
PrintConsole("ALREADY EXIST!!", ConsoleColor.Red);
else (if the value of the trade.OtherSID does not exist) you can write:
//add it
or
string myInsertQuery = "INSERT INTO test (id) Values ('"+trade.OtherSID+"')";
Shivanand ArurPosted Sep 29, 2012, 12:27 PM
What do you want to do if the value of the trade.OtherSID alread exists???
meinenrPosted Sep 29, 2012, 12:20 PM
string myConnectionString = "SERVER=localhost;" +
"DATABASE=test;" +
"UID=root;" +
"PASSWORD=;";
MySqlConnection connection = new MySqlConnection(myConnectionString);
MySqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT * FROM test";
MySqlDataReader Reader;
connection.Open();
Reader = command.ExecuteReader();
while (Reader.Read())
{
string row = "";
for (int i = 0; i < Reader.FieldCount; i++)
row += Reader.GetValue(i).ToString() + ", ";
Console.WriteLine(row);
}
connection.Close();
if(myConnectionString == "")
{
myConnectionString = "Database=test;Data Source=localhost;User Id=root;Password=";
}
MySqlConnection myConnection = new MySqlConnection(myConnectionString);
string myInsertQuery = "INSERT INTO test (id) Values ('"+trade.OtherSID+"')"; //here it must ask if the enrtie "trade.OtherSID" already exist in the column.
MySqlCommand myCommand = new MySqlCommand(myInsertQuery);
myCommand.Connection = myConnection;
myConnection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
Shivanand ArurPosted Sep 29, 2012, 12:12 PM
But you can still try out!!!
Shivanand ArurPosted Sep 29, 2012, 12:09 PM
These namespaces are usually used to work with MS-SQL Database...
Check out this link, on how to connect to MySql Database, how to insert data and how to get data from the table....
http://www.codeproject.com/Tips/423233/How-to-Connect-to-MySQL-Using-Csharp
Hope this helps you.
meinenrPosted Sep 29, 2012, 12:07 PM
I use MonoDevelop.
It will me a Bot soon.
I already used "using MySql.Data.MySqlClient", but there I have to install MySQL.data.dll and add it as referenc.
Shivanand ArurPosted Sep 29, 2012, 12:04 PM
and are you creating a C# ASP.NET Application or a Website or a C# Console Application???
Which technology are you using to create your application?
The mentioned namespaces are usually used in .Net applications while connecting to the MS-SQL Server.
using System.Data and System.Data.SqlClient gives us pre-defined classes like "SqlConnection", "SqlCommand", Datasets, "SqlDataAdapters", "SqlDataReaders" and many others.....
To access a MS-SQL Database, you have to use these namespaces.
meinenrPosted Sep 29, 2012, 11:58 AM
using System.Data;
using System.Data.SqlClient;
I have to add some reference to use them?
I used already the following lines:
using System;
using System.Collections.Generic;
using System.Linq;
using SteamKit2;
using MySql.Data.MySqlClient;
Shivanand ArurPosted Sep 29, 2012, 11:54 AM
"Select testName from test where testID = '" + trade.OtherSID +"'");"
This Query will return me the testName if it founds the ID from your table matching with the trade.OtherSID value. The "testName" in the above query is just an example to select some value from the table....
Complete Explanation of the above script
1. First I am creating a Sql Connection Object and adding the Connection String as a Parameter.
SqlConnection con = new SqlConnection("Connection String");
2. Then I am opening the Connection
con.Open();
3. Then I am creating a SQL Command Object passing a Select query with the " trade.OtherSID " to check its ID with the ID in the test Table.
SqlCommand cmd = new SqlCommand("Select testName from test where testID = '" + trade.OtherSID +"'");
4. Then I creating a Sql DataReader Object to read values which we get from the Select Query.
SqlDataReader dr = null;
dr = cmd.ExecuteReader();
while (dr.Read())
{
5. While reading the values from the DataReader, I am just checking if the reader object contains the value returned by the Select Statement
if(!String.IsNullOrEmpty(dr[0].ToString()))
{
Console.WriteLine(dr[0].ToString()); // Print If the reader has a value at its zero'th position
}
else
{
Console.WriteLine("Test Name Does Not Exist");
}
}
If you are creating a web application, then you will have to use the mentioned Namespaces otherwise for a Console Application use the following...
using System;
meinenrPosted Sep 29, 2012, 11:39 AM
Where is the line, which check if the ID (STEAM:1:0:XXXX....) exist in the column in my table?
I need to use all of this:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
Shivanand ArurPosted Sep 29, 2012, 11:28 AM
Example:
Your Table name: test
Columns in your table "test"
testID testName
STEAM:1:0:XXXX test123xyz...
STEAM:2:0:wxyz test456abc...
Suppose you want to get the TestName from the table and check whether the "STEAM:1:0:XXXX" ID exists in your table, you will have to do the following...
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(trade.OtherSID))
{
SqlConnection con = new SqlConnection("Connection String");// Setting the Connection with the SQL Database
con.Open();// Opening the Connection
// Create a Command Object and pass the Select query to get the field based on the trade.OtherSID
SqlCommand cmd = new SqlCommand("Select testName from test where testID = '" + trade.OtherSID +"'");
SqlDataReader dr = null;
dr = cmd.ExecuteReader();// Execute the Reader and get the value in the DataReader Object
while (dr.Read())// Read the values from the DataReader object until all the values are being read.
{
if(!String.IsNullOrEmpty(dr[0].ToString())); // Since there is only 1 value being achieved in the Datareader Object, Check if it is null or empty
{
Console.WriteLine(dr[0].ToString()); // Print If the reader has a value at its zero'th position
}
else
{
Console.WriteLine("Test Name Does Not Exist");// Print this if the reader object does not contain any value
}
}
con.Close(); // Close the Connection after completing the Transaction with the Data Source.
}
}
}
Hope this helps you out....
Let me know if you have any doubt!!!7
MARK THE ANSWER AS ACCEPTED IF IT HELPED YOU
meinenrPosted Sep 29, 2012, 11:08 AM
but if (!String.IsNullOrEmpty( trade.OtherSID )) ask if trade.OtherSID is "0" or "", but trade.OtherSID ist aways STEAM:1:0:XXXX....
I wanted to check if the entrie STEAM:1:0:XXXX.... exist in the column 'id' in my databe table 'test'.
Shivanand ArurPosted Sep 29, 2012, 10:24 AM
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
// trade.OtherSID should return a String Value otherwise you will have to use Convert.ToString(trade.OtherSID)
if (!String.IsNullOrEmpty( trade.OtherSID )) {
SqlConnection con = new SqlConnection("Connection String");
con.Open();
SqlCommand cmd = new SqlCommand("Insert into test (id)" + "Values(" + trade.OtherSID +")");
cmd.ExecuteNonQuery(); // Returns an integer value
con.Close();
}
}
}
This is just a sample example....
HOPE THIS HELPS YOU....
meinenrPosted Sep 29, 2012, 10:02 AM
string myInsertQuery = "INSERT INTO test (id) Values ('"+trade.OtherSID+"')";
How i can check if this entrie (trade.OtherSID) already exist?
Shivanand ArurPosted Sep 29, 2012, 9:19 AM
eg:
string Value = "Some value will come here";
if(!String.IsNullOrEmpty(Value))
{
Console.WriteLine(Value);
}
else
{
Console.WriteLine("Nothing Exists");
}