Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Need to select data from dbo.customers

Id
Name
Surname
tax

to class,becose need to use in my app.
Now I every time in form create string select from etc...
I dont want that.
Want to use data with class
Some exaples or link
Thank you

What I have tried:

<pre>select data from database with class c#
Posted
Updated 22-Nov-19 1:38am
Comments
Richard MacCutchan 22-Nov-19 4:42am    
What do you mean by "Want to use data with class"? Are you referring to the use of some framework?
Goran Bibic 22-Nov-19 5:25am    
Like this

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BSS
{
class Podaci_o_korisniku
{
private string Ovlasteno_Lice;

public string Name
{
get{return ovlasteno_Lice; }
set{ ovlasteno_Lice = value;}
}

public static void Podaci()
{
// Ovlasteno_Lice = Name;

SqlConnection con2 = new SqlConnection(Con);
con2.Open();
SqlCommand cmd = con2.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select ovlasteno_lice from dbo.podaci_o_korisniku";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
ovlasteno_lice.Items.Add(dr["ovlasteno_lice"].ToString());
}
con2.Close();
}


}

}
Goran Bibic 22-Nov-19 5:19am    
Need to select from database table Customer customer_name
Later across app to use customer_name from that class

Sounds like you are looking for an ORM, see: https://www.slant.co/topics/16667/~orms-for-c[^]

If your needs are simple you could also consider using LiteDB: best-databases-for-a-small-net-application~litedb[^]

Another option would be to use Linq to SQL: Walkthrough: Simple Object Model and Query (C#) | Microsoft Docs[^]
And also: Simple LINQ to SQL in C#[^]
 
Share this answer
 
v4
Comments
Goran Bibic 22-Nov-19 5:20am    
Not that. Another recommandition?
Thank you
Sounds like you want to create application which uses Entity Framework[^].

At the beginning, check out this: Code First to a New Database - EF6 | Microsoft Docs[^] and Code First to an Existing Database - EF6 | Microsoft Docs[^]
 
Share this answer
 
If you are just using ADO, and you have an example; why don't you alter the example to fit your needs?

Here is a basic version of what you want. It is not perfect and will take some tweaking. It also is not complete. But it is a start.
C#
public class Customer {
	public int Id { get; set; }
	public string Name { get; set; }
	public string Surname { get; set; }
	public decimal Tax { get; set; }

	public Customer () {}

	public Customer ReadOne(int customerID) {
		Customer c = null;
		string query = "SELECT id, Name, Surname, Tax FROM dbo.Customers WHERE id = @CustomerID";

		SqlConnection con = new SqlConnection("connection string");
		SqlCommand cmd = new SqlCommand(query, con);
		cmd.Parameters.AddWithValue("@CustomerID", customerID);

		con.Open()
	
		SqlDataReader reader = cmd.ExecuteReader();
		if (reader.HasRows) {
			c = new Customer();
			c.Id = reader.GetInt32(0);
			c.Name = reader.GetString(1);
			c.Surname = reader.GetString(2);
			c.Tax = reader.GetDecimal(2);
		}

		reader.Dispose();
		cmd.Dispose();
		con.Close();
		cmd.Dispose();
	}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900