How to call sql table' info into asp.net? - mysql

I want to connect and load any fields of table into a web form.more explain is that ,I want a SQL server connection in a web form and choose a database and then load all of fields and data type into a table.
for example i've a table with below features :
field type
id int
name nvarchar(50)
email nvarchar(50)
now i want to call them into web form not their values or records!
i hope explain it clearly
thanks for your solutions for help me...

In SQL you can make a query for sys tables ... you can try something like this
select tab.name, col.name, typ.name, col.max_length, * from sys.columns col
join sys.tables tab on col.object_id = tab.object_id
join sys.types typ on col.system_type_id = typ.system_type_id
where tab.name = 'Test'
Just change name of table and give it a go, you can use result of that query in your form

Related

SELECT * FROM db1 WHERE db1.table.value = db2.table.value

I'm working with mySQL db and trying to display the correct data for the user. In order to do that I check if the data that I call from one backend is equal to username from another backend like so
SELECT * FROM db1 WHERE db1.table.value = db2.table.value
Names of databases are A and B.
SELECT *
FROM `A.onboardings`
, `B.loginsystem`
WHERE onboardings.sales_email = loginsystem.username
The problem is I get an error A.A.onboardings doesn't exists and A.B.loginsystem doesn't exist pls help :(
You must use this form - from A onboardings
You have to put the backticks in the right pace, or else mysql things your table is called A.onboardings
As seen bleow the needs to be around the database and the table name
And the use of aliases helps to keep even in big queries a good overview and yu have to write less
"SELECT * FROM `A`.`onboardings` a1,`B`.`loginsystem` b1 WHERE a1.sales_email = b1.username"
Try this one( Change the query according to your DB name, table, and matching column name)
SELECT * FROM mydatabase1.tblUsers INNER JOIN mydatabase2.tblUsers ON mydatabase1.tblUsers.UserID = mydatabase2.tblUsers.UserID
The problem is that
`A.onboardings`
is not the same as
A.onboardings
The first is a table reference where there table name has a period in it. The second is for the onboardings table in database A.
In addition, you should be using JOIN!!!
SELECT *
FROM A.onboardings o JOIN
B.loginsystem ls
ON o.sales_email = ls.username;
If you feel compelled to escape the identifies -- which I do not recommend -- then:
SELECT *
FROM `A`.`onboardings` o JOIN
`B`.`loginsystem` ls
ON o.sales_email = ls.username;

Database Design - Multilingual Website

I'm having a multilingual website which it's users are able to have profile in different languages, for example each user could have his profile published in "English" and "French" and "Spanish", something like LinkedIn.
Now, I'm a user who is seeing the website in "English" language, so while I go to other members profile page, I should see that member profile in "English", if that profile is not available in that language, I should see that profile in that member "main_lang".
So I have a "members" table which has a column as "published_profile_langs", in this col the languages which each member has published his profile in is gonna be stored comma separated: "english,spanish", and "main_lang" col which is the user main language (his profile is definitely published in main_lang since we're asking for details on sign up step).
In another hand, members details are stored in different tables, such as "members_details_english", "members_details_spanish", "members_details_french".
I want to join my query, but it seems it's not possible in the way which I managed, currently I need to use 2 queries for loading the members details in the mentioned above scenario, my code in "members_profile.php" is:
// FIRST QUERY
$check_member = mysql_query("SELECT main_lang, profile_published_langs FROM members WHERE id = '$this_user_id'");
while($row = mysql_fetch_array($check_member)){
$this_main_lang = $row['main_lang'];
$this_profile_published_langs = $row['profile_published_langs'];
}
$this_profile_published_langs_arr = explode(',', $this_profile_published_langs);
if(!in_array($lang, $this_profile_published_langs_arr)) $lang = $this_main_lang;
$details_table = 'members_details_' . $lang;
// SECOND QUERY
$get_details = mysql_query("SELECT * FROM $details_table WHERE member_id = '$this_user_id'");
while($row_details = mysql_fetch_array($get_details)){
//blah blah
}
Is there any better way to achieve this? maybe someway to query once and not twice? any better database structure for this scenario?
I would appreciate any kind of help
Try considering language as just another entity in the database. Use the table members to store all data not dependant on the language, and have a second table with data that is; members_i18n - short for memebers_internationalization.
In the first table you can have a column called main_language_id and use the second table to store columns for data in different languages for each member by relating to it with member_id and language_id. This way you can fetch data for each member in all languages, just their main language, or any specific set of languages you need.
Plus, you won't need to use serialized data in your tables like profile_published_langs.
So a few example queries would be:
-- Main language
SELECT *
FROM member AS m
JOIN member_i18n AS mi
ON m.member_id = mi.member_id
AND m.main_language_id = mi.language_id
-- Specific language
SELECT *
FROM member AS m
JOIN member_i18n AS mi
ON m.member_id = mi.member_id
WHERE mi.language_id = 'eng'
-- All languages
SELECT *
FROM member AS m
JOIN member_i18n AS mi
ON m.member_id = mi.member_id
EDIT:
Personally, I usually use a third table with languages that looks like this:
CREATE TABLE `language` (
`language_id` char(3) NOT NULL,
`name` varchar(30) NOT NULL,
`code2` char(2) NOT NULL,
PRIMARY KEY (`language_id`)
);
-- Sample data
INSERT INTO `language` (`language_id`, `name`, `code2`) VALUES
('deu', 'Deutsch', 'de'),
('eng', 'English', 'en');
I found it to be very useful when printing out multilingual data.
EDIT 2:
So to fetch data for a user in the "current" language and their main language, just write a single query like this:
-- Current language (i.e. 'eng') + member's main language
SELECT *
FROM member AS m
JOIN member_i18n AS mi
ON m.member_id = mi.member_id
WHERE mi.language_id = m.main_language_id
OR mi.language_id = 'eng'
You'll end up with one or two rows, depending on the member's profile.
You can have a table members (id_user, mainlang, firstname, ...) and a table profile (id_user, language, and all data in that language). and for a user you have a row for each language. Your select is like this
select * from profile where id_user = $userId and language = $lang
I would probably build a user table, a language table and a mapping table
user_main - all usual columns, witha mail language column here
language table - whatever you want to keep, code etc, plus a column-
lang table name (equivalent to your user_details_spanish etc tables)
mapping table - user id, lanague id ( a row here means that that
particular user has a profile in that language)
Now, i would agree, that you might still need two queries, but I think its much more manageable since the table name is available from the lang table, and you might keep it in session (e.g. where you let the user choose from a drop down which language to switch to, the table name can be available there itself, so that you dont have to fetch it)...
Let me know if this direction of thought helps.. perhaps I can help further...

Accidently deleted all data in the 'master' database

I've used this script by accident on the 'master' database instead of a temp database.
sp_msforeachtable 'delete from ?'
Has it caused any harm? If so, how can I restore the data?
No it shouldn't have deleted anything (assuming you have no user tables in master).
Testing
exec sys.sp_MSforeachtable 'select ''?'''
Doesn't return anything for me. So it seems to exclude the system tables such as spt_values.
Edit: Indeed Looking at the definition of the procedure it does only include tables where OBJECTPROPERTY(o.id, N'IsUserTable') = 1
Martin Smith is right to say that sp_MSforeachtable does not delete system tables.
However, though we may think of tables such as spt_values and MSreplication_options as system tables, they are in fact user tables according to SQL Server.
When I run this query in my master database:
SELECT name, OBJECTPROPERTY(object_id, N'IsUserTable') AS IsUserTable
FROM master.sys.tables;
I see the following result set:
name IsUserTable
--------------------- -----------
spt_fallback_db 1
spt_fallback_dev 1
spt_fallback_usg 1
spt_monitor 1
MSreplication_options 1
So how was Stijn saved from a reinstall?
If you look at how sp_MSforeachtable is implemented, you will see it does something like this to select the tables to drop:
declare #mscat nvarchar(12)
select #mscat = ltrim(str(convert(int, 0x0002)))
SELECT *
from dbo.sysobjects o join sys.all_objects syso on o.id = syso.object_id
where OBJECTPROPERTY(o.id, N'IsUserTable') = 1 and o.category & #mscat = 0;
In my master database, this returns an empty result set.
The where clause applies a bitmask to the category column of table sysobjects to exclude tables that are not 'mscat'.
So the tables in the master database are protected not because they are system tables, but because they are 'Microsoft' tables.
This use of the category column is completely undocumented in Books Online All it has is a vague description:
Used for publication, constraints, and identity.
But the sysobjects table is deprecated anyway, so you shouldn't be using it. :)
An equivalent query using the supported view sys.tables would look like this:
SELECT *
FROM sys.tables
WHERE is_ms_shipped = 0;
In my master database, this also returns an empty result set.

design a stored procedure with multiple parameters and HAVING LIKE

I' m trying to write a stored procedure that will search a fairly simple database with
a USER table (user_id,name,...)
a USER_TYPE table (user_id,type_id) - multi to multi
a TYPE table (type_id,type_name)
a USER_GAME (user_id,game_id) -multi to multi
a GAME table (game_id,game_name)
A same user can have several games. Now, I want to be able to get the user according to a particular type and also according to a/some particular game(s), so that for example I can get all the user with, say type1, and with the games, say game2 and game5. I think I can get round the problem of several game names by passing them as a string parameter and do some kind of HAVING LIKE condition (I call get_user_spec('type1' , 'game3,game5') for example).
So far I get to that point:
CREATE DEFINER=`root`#`localhost` PROCEDURE `get_user_spec`(
IN inTypeName VARCHAR(50),
IN inGameName VARCHAR(150)
)
BEGIN
PREPARE statement FROM
"SELECT u.user_id,t.type_name,GROUP_CONCAT(g.game_name) AS game
FROM user u
INNER JOIN user_type ut
ON u.user_id=ut.user_id
INNER JOIN type t
ON ut.type_id=t.type_id
LEFT JOIN user_game ug
ON u.user_id=ug.user_id
LEFT JOIN game g
ON ug.game_id=g.game_id
WHERE t.type_name=?
GROUP BY u.user_id
HAVING game LIKE CONCAT('%',?,'%')
ORDER BY u.user_id";
SET #p1=inTypeName;
SET #p2=inGameName;
EXECUTE statement USING #p1,#p2;
END
But my real problem is that if I don't pass any game name, I then want to get all users with type1 (I then call get_user_spec('type1' , NULL). But I am then not getting anything as the procedure sees
HAVING game LIKE CONCAT('%',NULL,'%').
I hope that was clear enough. If anybody has any suggestions to get around that problem, I would be very grateful.
Thank you very much.
Change this line:
EXECUTE statement USING #p1,#p2;
to
EXECUTE statement USING #p1, ifnull(#p2, '');
This will cause the LIKE expression to be just '%%', which means "match everything"

Store procedure select all fields from One table using join

I am very frustrated from linq to sql when dealing with many to many relationship with the skip extension. It doesn't allow me to use joinned queries. Not sure it is the case for SQL server 2005 but I am currently using SQL Server 2000.
Now I consider to write a store procedure to fetch a table that is matched by two tables e.g. Album_Photo (Album->Album_Photo<-Photo) and Photo table and only want the Photos data so I match the Album's ID with Album_Photo and use that ID to match the photo. In the store procedure I am just fetch all the joinned data. After that in the linq to sql, I create a new Album object.
e.g.
var albums = (from r in result
where (modifier_id == r.ModifierID || user_id == r.UserID)
select new Album() {
Name = r.Name,
UserID = r.UserID,
ModifierID = r.ModifierID,
ID = r.ID,
DateCreated = r.DateCreated,
Description = r.Description,
Filename = r.Filename
}).AsQueryable();
I used the AsQueryable to get the result as a IQueryable rather than IEnumerable. Later I want to do something with the collection, it gives me this error:
System.InvalidOperationException: The query results cannot be enumerated more than once.
It sounds like you have a situation where the query has already executed by the time you are want to filter it later in your code.
Can you do something like...
var albums = (blah blah blah).AsQueryable().Where(filterClause) when you have enough info to process
what happens if you try albums.where(filter) later on in the code? Is this what you are trying?