Click here to Skip to main content
15,041,391 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hy To All,

My question is can we create a sql table without opening sqlserver 2005

i want to use ASP.NET 3.5 using C# code for creating the tables if this possible.
Posted

Yes, You can create SQL table without opening SqlServer.
   
You should use Create Table statement in your C# code.

using (SqlConnection con = new SqlConnection(yourConnectionStringHere)) //put your connection string here
{
con.Open();
try
{
    using (SqlCommand command = new SqlCommand(
    "CREATE TABLE Dogs1 (Weight INT, Name TEXT, Breed TEXT)", con))
    {
    command.ExecuteNonQuery();
    }
}
catch
{
    Console.WriteLine("Table couldn't be created.");
}
}


Ref: http://www.dotnetperls.com/sqlclient[^]
   
v2
I believe you don't want to use SSMS (Management Studio). Suggest you run below query (as command) through your application:

create table 'table name'
('col1' 'datatype1' 'null/not null',
'col2' 'datatype2' 'null/not null',
'col3' 'datatype3' 'null/not null',
)

Remember this is a very basic query, you might have to modify it to your needs. ALSO you (the account under which your application is running) should have permissions to create table on the DB.
   
v2
you can use create table query and can do so.

The only thing required is to make the connection with the instance of the sql server, as well as your application must have the appropiate permission to create new object.

G:):)gle is your friend to help you .........
   
v2
dont know if this could help but have you tried scripting?
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_Users
	(
	id int NOT NULL IDENTITY (1, 1),
	Name nvarchar(MAX) NULL,
	UserID nvarchar(MAX) NULL,
	CostCenter nvarchar(MAX) NULL,
	EmailAddress nvarchar(30) NULL
	)  ON [PRIMARY]
	 TEXTIMAGE_ON [PRIMARY]
GO
SET IDENTITY_INSERT dbo.Tmp_Users ON
GO
IF EXISTS(SELECT * FROM dbo.Users)
	 EXEC('INSERT INTO dbo.Tmp_Users (id, Name, UserID, CostCenter, EmailAddress)
		SELECT id, Name, UserID, CostCenter, CONVERT(nvarchar(30), EmailAddress) FROM dbo.Users WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_Users OFF
GO
DROP TABLE dbo.Users
GO
EXECUTE sp_rename N'dbo.Tmp_Users', N'Users', 'OBJECT' 
GO
ALTER TABLE dbo.Users ADD CONSTRAINT
	PK_Users PRIMARY KEY CLUSTERED 
	(
	id
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
COMMIT
select Has_Perms_By_Name(N'dbo.Users', 'Object', 'ALTER') as ALT_Per, Has_Perms_By_Name(N'dbo.Users', 'Object', 'VIEW DEFINITION') as View_def_Per, Has_Perms_By_Name(N'dbo.Users', 'Object', 'CONTROL') as Contr_Per
   

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