Register for your free account! | Forgot your password?

You last visited: Today at 05:49

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[Base]MSSQL for your server.

Discussion on [Base]MSSQL for your server. within the CO2 PServer Guides & Releases forum part of the CO2 Private Server category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 756
[Base]MSSQL for your server.

First of all you need to download Sql Server.


Well first of all we will make a Sql class.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class SqlWrapper
    {
    }
}
Then we define a namespace called System.Data.SqlClient.
Code:
using System.Data.SqlClient;
put this function as it will be called, when creating a new instance of the SqlWrapper class.
Code:
        public SqlWrapper()
        {
        }
This should be your code so far:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
    public class SqlWrapper
    {
        public SqlWrapper()
        {
        }
    }
}
We will put a string parameter in it as well. Call it connectionString.
Code:
        public SqlWrapper(string connectionString)
        {
        }
Now make a variable an SqlConnection called Connection.
Code:
SqlConnection Connection;
Now create a new instance of the SqlConnection class inside the previous function.
Code:
        public SqlWrapper(string connectionString)
        {
            Connection = new SqlConnection(connectionString);
        }
This should be the code so far:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
    public class SqlWrapper
    {
        public SqlConnection Connection;
        public SqlWrapper(string connectionString)
        {
            Connection = new SqlConnection(connectionString);
        }
    }
}
For updating and writing we will just use a simple Sql Command.
Make a new method called Command with the string parameter _command.
Code:
        public void Command(string _command)
        {
        }
First of all we will create the command using our previous connection.
Code:
SqlCommand Command = new SqlCommand(_command, Connection);
Now we put a try, catch and a finally block.
Code:
            try
            {
            }
            catch
            {
            }
            finally
            {
            }
Inside the try we will open the connection and execute our command.
You open the connection by doing this:
Code:
Connection.Open();
To execute the command we can use this:
Code:
Command.ExecuteNonQuery();
This should be our code so far in the method:
Code:
        public void Command(string _command)
        {
            SqlCommand Command = new SqlCommand(_command, Connection);
            try
            {
                Connection.Open();
                Command.ExecuteNonQuery();
            }
            catch
            {
            }
            finally
            {
            }
        }
We will catch an exception in the catch block, if any exception is thrown.
Code:
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
And in the finally we will close our connection.
Code:
Connection.Close();
This should be the command method:
Code:
        public void Command(string _command)
        {
            SqlCommand Command = new SqlCommand(_command, Connection);
            try
            {
                Connection.Open();
                Command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                Connection.Close();
            }
        }
For reading I will just give one example for reading string. you can make rest methods or do your own, if you feel it's necessary. I don't know other ways to read as this is the only way I have used and been knowing. If anybody knows other ways that's better or easier, feel free to enlighten me.

Create a new function for a string called ReadString.
Code:
        public string Read(string Select, string From, uint UID)
        {
        }
Select is the column and From is the table. UID is what the UID should be equal to.

Now put this 2 strings at the top of the function
Code:
string Value = "";
            string Command = "SELECT " + Select + " FROM " + From + " WHERE UID = " + UID;
Value is the value we get, which we will return and Command is the Sql Command to select a column from a table.

Now create a command using the previous connection again.
Code:
SqlCommand SQLCommand = new SqlCommand(Command, Connection);
And right under put a new variable for SqlDataReader called Reader.
Code:
SqlDataReader Reader;
Now make the try, catch and finally blocks like previous, but let the try block be clean.
Code:
            try
            {
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                Connection.Close();
            }
First of all we will open the connection.
Code:
Connection.Open();
Now, instead of executing the command directly, then we will execute a reader, using our command.
Code:
Reader = SQLCommand.ExecuteReader();
Now make a while loop as it will be looping until it has got through all of the objects that's supposed to be read. In this case we only have one.

Code:
                while (Reader.Read())
                {
                }
Now we got to make Value equal to the object that our reader has read.
Code:
Value = Reader[Select].ToString();
Make sure it's ToString as it's an object and that means we need to define the type of it either by ToString or type casting ex. (int)Reader[Select]; Anyways, after the while loop we will close the reader.
Code:
Reader.Close();
Now at the bottom of the function we will return the data.
Code:
return Value;
This should be the whole code of the SqlWrapper class.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
    public class SqlWrapper
    {
        public SqlConnection Connection;
        public SqlWrapper(string connectionString)
        {
            Connection = new SqlConnection(connectionString);
        }
        public void Command(string _command)
        {
            SqlCommand Command = new SqlCommand(_command, Connection);
            try
            {
                Connection.Open();
                Command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                Connection.Close();
            }
        }
        public string Read(string Select, string From, uint UID)
        {
            string Value = "";
            string Command = "SELECT " + Select + " FROM " + From + " WHERE UID = " + UID;

            SqlCommand SQLCommand = new SqlCommand(Command, Connection);
            SqlDataReader Reader;

            try
            {
                Connection.Open();
                Reader = SQLCommand.ExecuteReader();
                while (Reader.Read())
                {
                    Value = Reader[Select].ToString();
                }
                Reader.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                Connection.Close();
            }

            return Value;
        }
    }
}
About the connection string, that's basically your database informations, example:
Code:
Data Source=SERVERNAME;Initial Catalog=DATABASE;Integrated Security=True
SERVERNAME is the Sql Server name.
DATABASE is the Database name.

There is other properties like password etc. you can be using them as well.
Might take a look here:

I think that's it.

How can we use it then?

Example:
Code:
            SqlWrapper wrapper = new SqlWrapper("Data Source=SERVERNAME;Initial Catalog=DATABASE;Integrated Security=True");
            Char.Name = wrapper.Read("Name", "Characters", Char.UID);
BaussHacker is offline  
Old 07/08/2011, 08:43   #2
 
Spirited's Avatar
 
elite*gold: 12
Join Date: Jul 2011
Posts: 8,283
Received Thanks: 4,192

(not asking for anyone to agree, just sharing)
Spirited is offline  
Old 07/08/2011, 09:00   #3
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 756
Quote:
Originally Posted by Fаng View Post

(not asking for anyone to agree, just sharing)
BaussHacker is offline  
Old 07/08/2011, 09:33   #4
 
Spirited's Avatar
 
elite*gold: 12
Join Date: Jul 2011
Posts: 8,283
Received Thanks: 4,192
Quote:
Originally Posted by BaussHacker View Post
"Overall, MySQL has the edge in price as it is free, and performance can’t be conclusively shown to be any worse than MS SQL"

Really boils down to preference again xP like it always does.
Spirited is offline  
Old 07/08/2011, 09:38   #5
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 756
Quote:
Originally Posted by Fаng View Post
"Overall, MySQL has the edge in price as it is free, and performance can’t be conclusively shown to be any worse than MS SQL"

Really boils down to preference again xP like it always does.
But it's not better or worse, why not use MSSQL as it's already implented and can give you a lot more abilities, while working with C#? Such as things for your site etc. ? I see no reasons to use MySQL in this case, other than if you're hosting on Linux or websites using PHP. As peoples use C# here, then using MSSQL would be so much easier and it would literally not do anything other than improve your abilities of what you can. Let's say you make a site in ASP.NET, then it would be 100x times easier to use MSSQL as it's already implented in the membership etc. that way you can interact your server with your whole website. I know not many uses ASP.NET here and 99% uses PHP.
BaussHacker is offline  
Old 07/08/2011, 12:16   #6


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,126
Received Thanks: 2,518
Stored procedures Bauss for submitting data, very fast, moves data handling onto the server away from the application.

Personally i prefer the C# intergration for MSSQL and thats really the only reason i use it over MySQL, even if your using PHP you can get MSSQL plugins for PHP which allow you to use it almost as easily as MySQL.
Korvacs is offline  
Thanks
1 User
Old 07/08/2011, 15:54   #7
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 756
Quote:
Originally Posted by Korvacs View Post
Stored procedures Bauss for submitting data, very fast, moves data handling onto the server away from the application.

Personally i prefer the C# intergration for MSSQL and thats really the only reason i use it over MySQL, even if your using PHP you can get MSSQL plugins for PHP which allow you to use it almost as easily as MySQL.
I will look into that, thank you.
BaussHacker is offline  
Reply




All times are GMT +1. The time now is 05:50.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.