Reading and Updating Data Using SqlDataAdapter Question - data-access-layer

I'm looking for some examples on how to best use SqlDataAdapter to access and update data in my application.
Right now I have something like this:
SqlDataAdapter adapter;
DataSet myData = MyDataAccessClass.GetData("Select * from Students", ref adapter);
// change some data here and save changes
adapter.Update();
All of this occurs in code behind, and I dont really like it at all.
So, I'm trying to find a way to do something like this:
DataSet myData = MyDataAccessClass.GetStudents();
// change some data and save changes
MyDataAccessClass.SaveStudents(myData);
Where SaveStudents method still uses SqlDataAdapter to update db.
Any ideas on how to make this work or some pointers to best practices of doing
something like this are highly appreciated. Thank you.

That seems like a fairly basic Data Access Layer implementation, to me. Generally, I do it something like this:
public class MyDataAccessClass
{
private string ConnString;
public MyDataAccessClass()
{ //Get connection string from configuration file }
public MyDataAccessClass(string connString)
{ ConnString = connString; }
public DataSet GetAllStudents()
{
//your SQL Adapter code here...
}
}
One note that I'd make is that with so many ORM solutions (including just Entity Framework and Linq2Sql) available, you may want to consider using collections of objects instead of data-sets for your Data Representations. Then you can have a method like:
public void CreateUpdateStudent(Student student)
{
//update database
}
That's fairly subjective, I'll admit, but I find it preferable to using straight DataSets.

If you want to get update data using the sql-data-adapter then you could use these
Using System.Data.SqlClient;
SqlConnection con = new SqlConnection("Data Source=abcd-pc;Initial Catalog=user_info;Integrated Security=True");
SqlDataAdapter da = new SqlDataAdapter();
try
{
da.UpdateCommand = new SqlCommand("Update logindemo set password=#pswd where username=#uname",con);
da.UpdateCommand.Parameters.Add("#pswd", SqlDbType.VarChar).Value = txtpass.Text;
da.UpdateCommand.Parameters.Add("#uname", SqlDbType.VarChar).Value = txtusername.Text;
con.Open();
da.UpdateCommand.ExecuteNonQuery();
Label1.Text = "Data Updated";
con.Close();
}
catch
{
Label1.Text = "Unable To Connect";
}
I hope you understand how to update the data easily. It just like the example. You can use these type of example in Inserting into the Data, and Deleting the Data with using specific the command and sql query as it required.

Related

Using ASP.NET Core Web API WITHOUT Entity Framework

I need to build a Web API from ASP.NET Core without Entity Framework. It's an existing database that has some custom stored procedures and we do not want to use EF.
I searched this topic and can't find anything about it, is this even possible?
This is possible.
The first problem you will run into is getting the database connection string. You will want to import the configuration to do so. In a controller, it might look like this:
private readonly IConfiguration _configuration;
public WeatherForecastController(ILogger<WeatherForecastController> logger, IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;
}
Add using System.Data and using System.Data.SqlClient (you'll need NuGet for SqlClient) as well as using Microsoft.Extensions.Configuration. With access to the database, you are writing code "old style", for example:
[HttpGet]
[Route("[controller]/movies")]
public IEnumerable<Movie> GetMovies()
{
List<Movie> movies = new List<Movie>();
string connString = ConfigurationExtensions.GetConnectionString(_configuration, "RazorPagesMovieContext");
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Movie", conn);
DataSet ds = new DataSet();
sda.Fill(ds);
DataTable dt = ds.Tables[0];
sda.Dispose();
foreach (DataRow dr in dt.Rows)
{
Movie m = new Movie
{
ID = (int)dr["ID"],
Title = dr["Title"].ToString(),
ReleaseDate = (DateTime)dr["ReleaseDate"],
Genre = dr["Genre"].ToString(),
Price = (decimal)dr["Price"],
Rating = dr["Rating"].ToString()
};
movies.Add(m);
}
conn.Close();
return movies.ToArray();
}
The connection string name is in appsettings.json.
"ConnectionStrings": {
"RazorPagesMovieContext": "Server=localhost;Database=Movies;Trusted_Connection=True;MultipleActiveResultSets=true"
}
Yes it is possible. Just implement the API by yourself. Or here is also sample for the identity scaffold, without EF.
https://markjohnson.io/articles/asp-net-core-identity-without-entity-framework/
Just used Dapper as our ORM in a project rather than EF.
https://dapper-tutorial.net/
It is similar to ADO.Net, but it has some additionally features that we leveraged and it was really clean to implement.
I realize this is an old question, but it came up in a search I ran so I figured I'd add to the answers given.
First, if the custom stored procedures are your concern, you can still run them using Entity Framework's .FromSql method (see here for reference: https://www.entityframeworktutorial.net/efcore/working-with-stored-procedure-in-ef-core.aspx)
The relevant info is found at the top of the page:
EF Core provides the following methods to execute a stored procedure:
1. DbSet<TEntity>.FromSql(<sqlcommand>)
2. DbContext.Database.ExecuteSqlCommand(<sqlcommand>)
If you are avoiding Entity Framework for other reasons, it's definitely possible to use any database connection method you want in ASP.NET Core. Just implement your database connection methods using whatever library is relevant to your database and set up your controller to return the data in whatever format you want. Most, if not all, of Microsoft's examples return Entity Framework entities, but you can easily return any data format you want.
As an example, this controller method returns a MemoryStream object after running a query against an MS SQL server (note, in most cases where you want data returned it's my understanding that it should be a "GET" method, not "POST" as is done here, but I needed to send and use information in the HttpPost body)
[HttpPost]
[Route("Query")]
public ActionResult<Stream> Query([FromBody]SqlDto content)
{
return Ok(_msSqlGenericService.Query(content.SqlCommand, content.SqlParameters));
}
Instead of a MemoryStream, you could return a generic DataTable or a List of any custom class you want. Note that you'll also need to determine how you are going to serialize/deserialize your data.

LogIn form, SQL exception

I'm trying to make a simple program that has a log-in part, with a local database just for testing.And i keep getting an error when I try to open the connection to the SQL database.
private void logInButton_Click(object sender, EventArgs e)
{
MainMenu openMainMenu = new MainMenu();
SqlConnection sqlcon = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C: \Users\Nea Florin\Desktop\PlatformaTestare\PlatformaTestare\Server.mdf;Integrated Security=True;Connect Timeout=30");
sqlcon.Open();
SqlCommand cmd = new SqlCommand("Select * from Table Where username ='" + usernameTextBox.Text + "' and password = '" + passwrodTextBox.Text + "'");
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dtbl = new DataTable();
sda.Fill(dtbl);
if (dtbl.Rows.Count > 0)
{
openMainMenu.Show();
this.Hide();
}
else
MessageBox.Show("Wrong username or password!");
}
I get the error at sqlcon.Open();, and it is: "An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: An attempt to attach an auto-named database for file C: \Users\Nea Florin\Desktop\PlatformaTestare\PlatformaTestare\Server.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."
Well, the best advice I can give you is to google the error message. Keep in mind that if there is an error message it means that the problem is well known an as such it's a safe bet that someone have encountered it before you and managed to solve it. The first 4 results of this search are on stackoverflow and at least two of them have accepted answers, so I believe a little reasearch would have saved you a long time.
This is the best advice because it streaches far beyond your current problem. I firmly believe that good searching skills is the most important and most powerfull tools of a sotfware developer. I can assure you, no matter how much time you are developing software, almost every exception you get, someone else have already solved and posted the solution somewhere, you only need to find it.
Now, as for the code it self - You have some major problems other then the exception you are asking about:
Concatenating strings into sql statements instead of using parameters expose your code to SQL injection attacks. This is a very serious threat that is extremely easy to fix.
Using insntances of classes that implements the IDisposable interface without properly disposing them may lead to memory leak. Read about the using statement and make it a habit to use it every time it's possible.
Exception handling. Currently, if your database can't be reached, you get an exception and your program crash. You should use a try...catch block anywhere you can't control in code to let your program end gracefuly instead. (Don't ever use try...catch for things you can do in code such as validate user input or checking division by zero - only for things that are beyon your control such as database availability.)
Having said all that, your code should look something like this:
private void logInButton_Click(object sender, EventArgs e)
{
using (var sqlcon = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|C:\Users\Nea Florin\Desktop\PlatformaTestare\PlatformaTestare\Server.mdf;Integrated Security=True;Connect Timeout=30"))
{
sqlcon.Open();
using (var cmd = new SqlCommand("Select 1 from Table Where username = #userName and password = #password"))
{
cmd.Parameters.Add("#userName", SqlDbType.NVarChar).Value = usernameTextBox.Text;
cmd.Parameters.Add("#password", SqlDbType.NVarChar).Value = passwrodTextBox.Text;
using (var dtbl = new DataTable())
{
using (var sda = new SqlDataAdapter(cmd))
{
sda.Fill(dtbl);
}
if (dtbl.Rows.Count > 0)
{
var openMainMenu = new MainMenu();
openMainMenu.Show();
this.Hide();
}
}
else
{
MessageBox.Show("Wrong username or password!");
}
}
}

Why calling DataTable.Clear() then DBDataAdapter.Update(DataTable) does not clear the table in database?

I try to use the code sample in DBDataAdapter.Update Method to clear a table in a database.
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM WebCam", connection);
DataTable table = new DataTable();
adapter.Fill(table);
table.PrimaryKey = new DataColumn[] { table.Columns["Date"] };
//table.Rows[0]["Date"] = System.DateTime.Now; //It's OK to modify a row
table.Clear(); //But it is not working to clear the table
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.Update(table);
}
I can add new rows or modify existing rows, and the changes can be committed to the database, but if I try to empty the table, the change to 'table' can not be committed to the database, also, no exception is thrown.
Do I miss something?
u should get error on this line:
table.PrimaryKey = new DataColumn[] { table.Columns["Date"] };
this would mean that u have catched the exception somewhere else, maybe from where the function is being called, and not showing the error in a messege box.
Another reason might be because you are using words like Table, adapter etc ... my guess is that you might be overloading some reserved words functionalities.

Accessing SSAS cubes using WCF data services

I have a requirement by which I need to retrieve data from SSAS cubes and send it over to OData. How can that be done? Any help will be greatly appreciated. Any pointers to sample code will also be greatly appreciated.
Thanks and regards,
Venkatesh. S
you can use ADOMD.Net to execute the query and send the result with wcf. Below is the class that I usually use.
class MDXQueryEngine : IMDXQueryEngine
{
private readonly string _serverName;
private readonly string _initalCatalog;
public MDXQueryEngine(string serverName, string initalCatalog)
{
_serverName = serverName;
_initalCatalog = initalCatalog;
}
public TResult Execute<TResult>(MDXQuery query, Func<IDataReader, TResult> work)
{
var connectionString = string.Format("Data Source={0}; Initial Catalog={1}", _serverName, _initalCatalog);
TResult result;
using (var conn = new AdomdConnection(connectionString))
{
conn.Open();
using(var cmd = conn.CreateCommand())
{
cmd.CommandText = query.Expression;
using (var reader = cmd.ExecuteReader())
{
result = work(reader);
}
}
}
return result;
}
}
You can query SSAS cubes using LINQ and ADO.NET Entity Framework.
You will need SSAS Entity Framework Provider in order to be able to do that:
http://agiledesignllc.com/products
This option makes building WCF Data Services straightforward and trivial.
I assume it is SSAS cubes. Not sure about specific frameworks as such to do this.
But one possible solution:
Wrap the Cube (MDX) with a store procedure. (Access SSAS with MDX from SP)
You can create WCF data service, add an EM and specify stored procedure for EM. You should be able to expose them as oData.

Best method for Populating DataSet from a SQLDataReader

I am working on a DAL that is getting a DataReader Asynchronously.
I would like to write a single method for transforming the DataReader into a DataSet. It needs to handle different schema so that this one method will handle all of my fetch needs.
P.S. I am populating the SQLDataReader Asynchronously, please don't give answers that are getting rid of the DataReader.
DataTable.load() can be used for a generic approach.
do {
var table = new DataTable();
table.Load(reader);
dataset.Tables.Add(table);
} while(!reader.IsClosed);
Try DataSet.Load(). It has several overloads taking an IDataReader.
If for some reason the Load method fails, here is a manual way to do it:
DataTable dt = new DataTable();
dt = sdr.GetSchemaTable();
//dt.Constraints.Clear();
//dt.PrimaryKey = null;
//dt.BeginLoadData();
if (sdr.HasRows)
{
DataRow row;
while (sdr.Read())
{
row = dt.NewRow();
sdr.GetValues(row.ItemArray);
dt.Rows.Add(row);
}
Another way is to use SqlTableAdapter:
var adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds);