C
C#14mo ago
Yasaki Kondo

❔ I'm trying to connect to a database from LinQ to SQL

static void Main(string[] args)
{
using (var db = new CustomerDBDataContext(@"DataSource=.;Initial Catalog=northwind;Intergrated Security=True"))
{
var customers = db.Customers().ToList();
foreach (var x in customers)
{
Console.WriteLine(x.CompanyName);
}
}
}
static void Main(string[] args)
{
using (var db = new CustomerDBDataContext(@"DataSource=.;Initial Catalog=northwind;Intergrated Security=True"))
{
var customers = db.Customers().ToList();
foreach (var x in customers)
{
Console.WriteLine(x.CompanyName);
}
}
}
57 Replies
SinFluxx
SinFluxx14mo ago
What's the error actually referencing?
Yasaki Kondo
Yasaki KondoOP14mo ago
It said that the error referencing 'CustomerDBDataContext' could not be found in a type or a namespace I'm currently learning how to connect to a database by using linq to sql but the teacher haven't teach me enough anything on how to use it yet Idk how to show the data on screen you got any ideas?
SinFluxx
SinFluxx14mo ago
Can you show the error? It sounds like you're missing a using statement in that file
Yasaki Kondo
Yasaki KondoOP14mo ago
No description
SinFluxx
SinFluxx14mo ago
that doesn't really give any extra information 🙂
Yasaki Kondo
Yasaki KondoOP14mo ago
No description
SinFluxx
SinFluxx14mo ago
click on CustomerDBDataContext, then press ctrl + . that should give you a list of possible fixes, I would imagine one is to add a using statement for the namespace where you've created CustomerDBDataContext
Yasaki Kondo
Yasaki KondoOP14mo ago
yea that doesn't help much
SinFluxx
SinFluxx14mo ago
?
Angius
Angius14mo ago
Does this class, actually, y'know, exist?
Yasaki Kondo
Yasaki KondoOP14mo ago
no? I'm trying to follow instruction but I hit a dead end on what to do next
Angius
Angius14mo ago
Then how do you want to use it...?
Yasaki Kondo
Yasaki KondoOP14mo ago
I want to use the database to extract data out so I can print them on screen
Angius
Angius14mo ago
Cool, read up on how to use EF Core properly, then
Yasaki Kondo
Yasaki KondoOP14mo ago
EF core?
Angius
Angius14mo ago
That's what you're trying to use to get data from the database, no?
Yasaki Kondo
Yasaki KondoOP14mo ago
wait so I have to create a getter setter for the database?
Angius
Angius14mo ago
Entity Framework Core? You have to create the context, first of all
Yasaki Kondo
Yasaki KondoOP14mo ago
well the teacher instruct me to connect to the database using linq to sql
Angius
Angius14mo ago
Or, if you want to start from an existing database, you can generate it Ah, oof, linq2sql... Well, look up the docs of that, then
Yasaki Kondo
Yasaki KondoOP14mo ago
damn, all of the tutorials on youtube showing how to use linq to sql by using a windows form something, I'm only here to use net core or frame work
Angius
Angius14mo ago
Code is code, it's not that different Do adapt it to your setup Or, as I said, read the documentation instead of looking up YT tuts
Yasaki Kondo
Yasaki KondoOP14mo ago
I'm better suited when it comes to visual learner
Angius
Angius14mo ago
Tough
Yasaki Kondo
Yasaki KondoOP14mo ago
I wish some python knowledge could translate these c# code xD but they are not on the same subject rn ;v; I think I fixed it with switch from net core to framework but a new problem arise
Yasaki Kondo
Yasaki KondoOP14mo ago
@SinFluxx got any ideas?
No description
No description
SinFluxx
SinFluxx14mo ago
In your connection string I believe it should be "Data Source" not "DataSource"
Yasaki Kondo
Yasaki KondoOP14mo ago
look's like it works? it doesn't doesn't show any data for some reason how do I check the data inside manually? in SQL there's data but in C# it doesn't show any dafuq?
Yasaki Kondo
Yasaki KondoOP14mo ago
How? @sn0wgirl_97
No description
Yasaki Kondo
Yasaki KondoOP14mo ago
I need your help on this
SG97
SG9714mo ago
how did you add it into Server Explorer
Philémon
Philémon14mo ago
@Yasaki Kondo (Kei Kanna) Do you know how to find the connection string of your DB? And once you got the connection string, this link explains the basic steps to connect to your DB. Other links on this page guide you to fetch the DB data. https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql/linq/how-to-connect-to-a-database
How to: Connect to a Database - ADO.NET
Learn how to use DataContext to connect to a database in LINQ to SQL. Refer to these examples to use DataContext to connect to a database and to get rows.
Yasaki Kondo
Yasaki KondoOP14mo ago
I open sql and add in northwind with the given file (northwind.sql) and log in with the same account and try to add connection to it
Philémon
Philémon14mo ago
Do you know your connection string?
Yasaki Kondo
Yasaki KondoOP14mo ago
uh, let me check
Yasaki Kondo
Yasaki KondoOP14mo ago
No description
Yasaki Kondo
Yasaki KondoOP14mo ago
uh this pop up
Philémon
Philémon14mo ago
You can open it as admin but i don't think that s what you need to do
Yasaki Kondo
Yasaki KondoOP14mo ago
didn't I already give all permission to all users I created?
Philémon
Philémon14mo ago
I think i can help you, maybe, but first i need to understand I know that using databases can be hard if you are not used to it
Yasaki Kondo
Yasaki KondoOP14mo ago
It's my first time working with one :3 currently I have no connection rn
Philémon
Philémon14mo ago
First of all, i see on the previous image that you are opening the Sql express DB "sqlexpress.northwind", But on the other end i see in your solution that your dbml talks about DataClasses1. So my first question would be : where are the data you want to fetch?
Yasaki Kondo
Yasaki KondoOP14mo ago
from the northwind.mdf but at first it wasn't showing any data so I'm try something else
Philémon
Philémon14mo ago
So you made the connection with northwind successfully ?
Yasaki Kondo
Yasaki KondoOP14mo ago
this was probably an empty List I think?, it show the collumns and rows but I see no data on it
Philémon
Philémon14mo ago
No, what you see is the DB So, when you connect your Visual studio on your Sql database, the two are linked okay But if you want to fetch data from your database with your code, you need to make also the "connection" in your code, and that's why we use a connection string
Yasaki Kondo
Yasaki KondoOP14mo ago
oh it's something like using ("")?
Philémon
Philémon14mo ago
So what you need first is to get your connection string, i think. So what you can do is click on SQL server explorer and find in the tree where your northwind db is
Yasaki Kondo
Yasaki KondoOP14mo ago
find the tree? sorry, I still learning english xD
Philémon
Philémon14mo ago
the hierarchy tree wait a second I will give you a screenshot
Yasaki Kondo
Yasaki KondoOP14mo ago
ok
Philémon
Philémon14mo ago
See if you can find something like this. Explanation : 1) I am on "PHILDESKTOP(SQL SERVER)" thats basically my SQL Server 2) I am in the folder Databases 3) And in the previous folder, there are my databases. What you need when you find it, is left click on the arrow next to your database name (northwind) so that it "expands" its content, and then you right click on the name northwind and you click Properties
No description
Philémon
Philémon14mo ago
Here, Sandbox is the name of my db (northwind for you) If you succeed in seeing the Properties of your Database, then there is a property in the "Properties" window that says "Connection string" in one column and in the other column, there is your connection string. It will not exactly be the same for you, i must tell, because I use sql server and you seem to use SQL Express
Philémon
Philémon14mo ago
Probably this actually ?
No description
Yasaki Kondo
Yasaki KondoOP14mo ago
that's the just database I connected to through account
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server