Click here to Skip to main content
15,888,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone, Am working with c# and Sql server 2005 , I want to create a database using with c# from existing database ...
but i have find some error in my code, ie.

C#
SqlConnection sqlcon = new SqlConnection("Data Source=.\\SQLEXPRESS;initial catalog=;Integrated Security=SSPI;User Instance=True");
sqlcon.Open();
SqlCommand sqlcmd = new SqlCommand("SELECT * INTO imgsoftware.dbo.ram4 FROM TBLADMIN", sqlcon);
sqlcmd.ExecuteNonQuery();
sqlcon.Close();


error is: Database 'imgsoftware' does not exist. but i have this database


if someone have any solution of this problem tell me..

Thanx ....
Posted
Updated 24-Jan-12 20:01pm
v2

Hi,

http://www.functionx.com/csharp1/adonet/Lesson04.htm

Hope this url is helpfull to you.
 
Share this answer
 
v2
Comments
manish the error 25-Jan-12 2:01am    
Thanx for it , But my problem is , i want to create a database table from another existing database table... and in this url not have any information about it,,
please send me another solution ...
Hi,

You need to execute CREATE TABLE....AS query for that.

check this link.

you also need to specify your database name along with your table name.

hope it will help you,

thanks
-Amit.
 
Share this answer
 
v2
Comments
manish the error 25-Jan-12 4:39am    
thnx for rply , but i want to create a database table from another database table ie.
run this code in your sql server manegment studio===
SELECT * INTO imgsoftware.dbo.ram4 FROM ram.dbo.TBLADMIN


i want to execute this code in c#, if you have any solution please send me...

Thnx
AmitGajjar 25-Jan-12 4:46am    
it works fine in Management studio. what error you are getting with C# code ?
AmitGajjar 25-Jan-12 4:47am    
are you sure you have rights on imgsoftware database ?
AmitGajjar 25-Jan-12 4:48am    
using windows authentication are you able to do everything on imgsoftware database ? please check permission.
manish the error 25-Jan-12 4:53am    
yes i am working with full authentication in windows bt it's not working...
i hav got a error eg.
Database 'imgsoftware' does not exist. but i have this database

if u have another solution for this send me.
if u have another way for doing this send me , i want to create a database table from other database...

thnx
Create table DatabaseName.schemaName.tableName
.............
...............

eg.

Create table Test.dbo.Testing(id int)
 
Share this answer
 
Comments
manish the error 25-Jan-12 4:42am    
thnx for rply ...
but please read this question properly....

and if you have any solution please send me it

thnx
SqlConnection sqlcon = new SqlConnection("Data Source=.\\SQLEXPRESS;initial catalog=;Integrated Security=SSPI;User Instance=True");
sqlcon.Open();
SqlCommand sqlcmd = new SqlCommand("SELECT * INTO imgsoftware.dbo.ram4 FROM TBLADMIN", sqlcon);

SqlCommand sqlcmd = new SqlCommand("SELECT * INTO ram4 FROM TBLADMIN", sqlcon);
--This is not a create table command

SqlCommand sqlcmd = new SqlCommand("Create Table yourTable(Id int)", sqlcon);

sqlcmd.ExecuteNonQuery();
sqlcon.Close();


Thanks
Mamun
 
Share this answer
 
Comments
manish the error 25-Jan-12 4:46am    
give this command in your sql server management studio......
its working properly....

SELECT * INTO imgsoftware.dbo.ram4 FROM ram.dbo.TBLADMIN

i want to do it in C#...

thanx......
Hi,

try to set catalog property of connection string.

Regards
Robert
 
Share this answer
 
Comments
manish the error 25-Jan-12 4:48am    
thnx for the rply sir ...
but i have seted all thise property bt its not working ,,,, if u have any ather solution please send me...



thnx again

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