First of all you need to download Sql Server.
[Only registered and activated users can see links. Click Here To Register...]
Well first of all we will make a Sql class.
Then we define a namespace called System.Data.SqlClient.
put this function as it will be called, when creating a new instance of the SqlWrapper class.
This should be your code so far:
We will put a string parameter in it as well. Call it connectionString.
Now make a variable an SqlConnection called Connection.
Now create a new instance of the SqlConnection class inside the previous function.
This should be the code so far:
For updating and writing we will just use a simple Sql Command.
Make a new method called Command with the string parameter _command.
First of all we will create the command using our previous connection.
Now we put a try, catch and a finally block.
Inside the try we will open the connection and execute our command.
You open the connection by doing this:
To execute the command we can use this:
This should be our code so far in the method:
We will catch an exception in the catch block, if any exception is thrown.
And in the finally we will close our connection.
This should be the command method:
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.
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
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.
And right under put a new variable for SqlDataReader called Reader.
Now make the try, catch and finally blocks like previous, but let the try block be clean.
First of all we will open the connection.
Now, instead of executing the command directly, then we will execute a reader, using our command.
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.
Now we got to make Value equal to the object that our reader has read.
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.
Now at the bottom of the function we will return the data.
This should be the whole code of the SqlWrapper class.
About the connection string, that's basically your database informations, example:
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: [Only registered and activated users can see links. Click Here To Register...]
I think that's it.
How can we use it then?
Example:
[Only registered and activated users can see links. Click Here To Register...]
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
{
}
}
Code:
using System.Data.SqlClient;
Code:
public SqlWrapper()
{
}
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
public class SqlWrapper
{
public SqlWrapper()
{
}
}
}
Code:
public SqlWrapper(string connectionString)
{
}
Code:
SqlConnection Connection;
Code:
public SqlWrapper(string connectionString)
{
Connection = new SqlConnection(connectionString);
}
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);
}
}
}
Make a new method called Command with the string parameter _command.
Code:
public void Command(string _command)
{
}
Code:
SqlCommand Command = new SqlCommand(_command, Connection);
Code:
try
{
}
catch
{
}
finally
{
}
You open the connection by doing this:
Code:
Connection.Open();
Code:
Command.ExecuteNonQuery();
Code:
public void Command(string _command)
{
SqlCommand Command = new SqlCommand(_command, Connection);
try
{
Connection.Open();
Command.ExecuteNonQuery();
}
catch
{
}
finally
{
}
}
Code:
catch (Exception e)
{
Console.WriteLine(e);
}
Code:
Connection.Close();
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();
}
}
Create a new function for a string called ReadString.
Code:
public string Read(string Select, string From, uint UID)
{
}
Now put this 2 strings at the top of the function
Code:
string Value = "";
string Command = "SELECT " + Select + " FROM " + From + " WHERE UID = " + UID;
Now create a command using the previous connection again.
Code:
SqlCommand SQLCommand = new SqlCommand(Command, Connection);
Code:
SqlDataReader Reader;
Code:
try
{
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
Connection.Close();
}
Code:
Connection.Open();
Code:
Reader = SQLCommand.ExecuteReader();
Code:
while (Reader.Read())
{
}
Code:
Value = Reader[Select].ToString();
Code:
Reader.Close();
Code:
return Value;
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;
}
}
}
Code:
Data Source=SERVERNAME;Initial Catalog=DATABASE;Integrated Security=True
DATABASE is the Database name.
There is other properties like password etc. you can be using them as well.
Might take a look here: [Only registered and activated users can see links. Click Here To Register...]
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);