Hello! I have problem...i need to include SQL connection class in other file (also class)
i tried to use "using" keyword but no success..
(Compiler Error Message: CS0246: The type or namespace name 'sql_lib' could not be found)
So the idea is to create a class for atomization for sql queries....and i want to call functions from this class in some files...example of what i tried:
sql_lib.cs
namespace sql_lib
{
public class sql_lib
{
public static DataSet ds = new DataSet();
public static string conStr;
public sql_lib()
{
conStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename='|DataDirectory|\dbUsers.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True";
}
public static DataSet sql_query(string q)
{
SqlConnection conn = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = q;
cmd.Connection = conn;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
conn.Open();
adapter.Fill(ds, "tblUsers");
conn.Close();
return ds;
}
public static void fetch_results(DataSet ds)
{
foreach (DataRow row in ds.Tables[0].Rows)
{
HttpContext.Current.Response.Write("<tr>");
HttpContext.Current.Response.Write("<td>" + row["username"].ToString() + "</td> ");
HttpContext.Current.Response.Write("<td>" + row["password"].ToString() + "</td> ");
HttpContext.Current.Response.Write("<td>" + row["email"].ToString() + "</td> ");
HttpContext.Current.Response.Write("</tr>");
}
}
}
}
show_users.aspx.cs
using sql_lib;
using System;
public partial class search_users : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = sql_lib.sql_lib.sql_query("SELECT * FROM tblUsers");
sql_lib.sql_lib.fetch_results(ds);
}
...........
i tried to use "using" keyword but no success..
(Compiler Error Message: CS0246: The type or namespace name 'sql_lib' could not be found)
So the idea is to create a class for atomization for sql queries....and i want to call functions from this class in some files...example of what i tried:
sql_lib.cs
namespace sql_lib
{
public class sql_lib
{
public static DataSet ds = new DataSet();
public static string conStr;
public sql_lib()
{
conStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename='|DataDirectory|\dbUsers.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True";
}
public static DataSet sql_query(string q)
{
SqlConnection conn = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = q;
cmd.Connection = conn;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
conn.Open();
adapter.Fill(ds, "tblUsers");
conn.Close();
return ds;
}
public static void fetch_results(DataSet ds)
{
foreach (DataRow row in ds.Tables[0].Rows)
{
HttpContext.Current.Response.Write("<tr>");
HttpContext.Current.Response.Write("<td>" + row["username"].ToString() + "</td> ");
HttpContext.Current.Response.Write("<td>" + row["password"].ToString() + "</td> ");
HttpContext.Current.Response.Write("<td>" + row["email"].ToString() + "</td> ");
HttpContext.Current.Response.Write("</tr>");
}
}
}
}
show_users.aspx.cs
using sql_lib;
using System;
public partial class search_users : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = sql_lib.sql_lib.sql_query("SELECT * FROM tblUsers");
sql_lib.sql_lib.fetch_results(ds);
}
...........