Insert into RemoveFile table in MSI - file

In order to automate some actions in MSI I have been trying to insert into RemoveFile table using JScript and keep getting errors and since there is not much error description i am not able to figure out the issue in query and the only error that i get when i try to debug using csript is -2147467259 OpenView,SQL,
This is the query that i am using to insert into removefile table, Can anyone please help me figuring out the issue.
"INSERT INTO `RemoveFile` (`FileKey`, `Component_`, `FileName`, `DirProperty`, `InstallMode`) VALUES (`_142D31F52C744D6FB945F01BA06EEFB3`, `C__931358B017AE83C769F5CB9E95BD2401`, `Product version 2.0.lnk`, `DesktopFolder`, 1)

Have you opened the view on the installer database?
var installer = WScript.CreateObject("WindowsInstaller.Installer");
var database = installer.OpenDatabase(<your-product.msi>,
msiOpenDatabaseModeTransact);
sql = "INSERT INTO `RemoveFile` "
+ "(`FileKey`, `Component_`, `FileName`, "
+ "`DirProperty`, `InstallMode`) "
+ "VALUES ('_142D31F52C744D6FB945F01BA06EEFB3', "
+ "'C__931358B017AE83C769F5CB9E95BD2401', "
+ "'Product version 2.0.lnk', 'DesktopFolder', 1)";
view = database.OpenView(sql);
view.Execute();
view.Close();

Related

vb.net SQL command to sort SQL table

I have the following code in vb.net to sort a SQL Server table alphanumerically. It works in SQL Server Management Studio, but when I run the code in vb.net nothing happens.
Do I need to update the SQL Server table and how? Can I even do this directly from vb.net?
Dim opdragsorteeraplhanumeries As New SqlCommand
konneksie.ConnectionString = "Data Source=GIDEON-E-LAPTOP\SQLEXPRESS2014;Initial Catalog=SkeduleringDatabasis;Integrated Security=True"
konneksie.Open()
opdragsorteeraplhanumeries.Connection = konneksie
opdragsorteeraplhanumeries.CommandText = "SELECT * FROM Oesskattings " & _
"ORDER BY " & _
"CASE WHEN ISNUMERIC(blokno) = 1 THEN right(Replicate('0',21) + blokno, 21) " & _
"WHEN ISNUMERIC(blokno) = 0 then Left(blokno + Replicate('',21), 21) " & _
" ELSE blokno " & _
" End "
opdragsorteeraplhanumeries.ExecuteNonQuery()
konneksie.Close()
MsgBox("Alphanumeries gesorteer")
Regards
There's no sorting of a table going on there. Whether that code is executed in SSMS or not, what it is doing is getting the data from the table and sorting it. The table remains exactly as it was. If it is your intention to actually change the order of the data in the table then you'd have to move the data from original table into a temp table and then back again, doing the ordering on one of the two steps.
Would there really be any point to that though? Why exactly do you think you need the table sorted a particular way and, if that's really what you need, why isn't it like that to begin with? More likely you should simply sort the data when you retrieve it or perhaps create a view that does the sorting and get your data from there.

How to create a Database user in Entity Framework

Been working on my first Entity Framework project. As part of the project I am going to be creating a number of SSRS reports. In order to connect to the database I need to have a Reports user that will only access to the specific database on the server. In the past i have always written a script to add Database users but I want to know is there a way that i can do this using Entity Framework instead?
Assuming your user already has a login defined at the SQL Server level (Security > Logins), you can call the following method from your DB initializer seed method to add the user to the database:
private void AddDbUser(MyDataContext myDB)
{
string accountDomainName = "AccountDomainName"; // replace with user's login domain
string accountLoginID = "AccountLoginID"; // replace with user's login ID
string sql =
"USE [MyDB]" +
"CREATE USER [MyNewUser] FOR LOGIN [" + accountDomainName + "\\" + accountLoginID + "]" +
"ALTER AUTHORIZATION ON SCHEMA::[db_datareader] TO [" + accountLoginID + "]" +
"ALTER AUTHORIZATION ON SCHEMA::[db_datawriter] TO [" + accountLoginID + "]" +
"EXEC sp_addrolemember N'db_datawriter', N'" + accountLoginID + "'" +
"EXEC sp_addrolemember N'db_datareader', N'" + accountLoginID + "'";
myDB.Database.ExecuteSqlCommand(sql);
}
The exact SQL needed is dependent on your configuration. To have SQL Server generate the SQL for your scenario, you could open the add user dialog in SSMS (Database > Users > New User...), fill out the fields, and click the "Script" button at the top instead of hitting OK at the bottom. Note that any "GO" lines will need to be removed from the generated script before pasting it into the method above.
You would need to tell EF to use the appropriate stored procedures to do so. You could also wrap these up in a sproc of your own that wraps the relevant commands. There is no native "CreateReportsUser" type method within EF that I know of.
Edit: I probably should have provided this reference to be a "complete" answer. Apologies.
Here's how you can do what I recommend: How to call Stored Procedure in Entity Framework 6 (Code-First)?

The SQL statement works properly in SQL Server database, but errors in vb.net

I need to insert new record into a SQL Server database, but get
Incorrect syntax error
The strange thing is when I try to query the same statement in SQL Server itself, it works properly.
The code in vb.net is as follows:
insertSql = "INSERT INTO Seg_LINE VALUES (" & OBJECTID & ", 'test" + "', '" + "test" + "','" + DrainName + "'," & UID & ")"
logger.Info("insert sql = " + insertSql)
Dim cmdInsert As New SqlClient.SqlCommand(insertSql, Sqlconnection)
cmdInsert.ExecuteNonQuery()
The OBJECTID and UID are number parameters.
I cannot figure out what's wrong with my code, I am using vb.net(vs2102).
Most likely you have a DrainName value with a single quote in it. You're lucky the query is just failing, and not executing unwanted commands on your DB server. Don't use string concatenation like that to build queries! You need to use query parameters, like this:
insertSql = "INSERT INTO Seg_LINE VALUES (#ObjectID, 'test', 'test', #DrainName, #UID)"
logger.Info("insert sql = " + insertSql)
Dim cmdInsert As New SqlClient.SqlCommand(insertSql, Sqlconnection)
'I'm guessing at these parameter types. Use the actual db types of the columns
cmdInsert.Parameters.Add("#ObjectID", SqlDbType.Int).Value = OBJECTID
cmdInsert.Parameters.Add("#DrainName", SqlDbType.NChar, 50).Value = DrainName
cmdInsert.Parameters.Add("#UID", SqlDbType.Int).Value = UID
cmdInsert.ExecuteNonQuery()
Changing the code this way will also likely fix your syntax error.

Insert Script running in web server for Inserting sql data

I have a script which is running perfectly in local host. the code is this
string sql = "insert into Usertable ";
sql += "values(" + mVendid + ", '" + usrname + "','" + usrpass + "', CONVERT(datetime, " + datecreation + ", 103)" + "," + createdby + ")";
The values are (1,'sa','ee','05/18/2013', 1)
This is also running fine if I run the same in sql server. But I am having only problem when I am inserting the date field. If I remove teh date field in insert statement the code is running absolutely fine
But while running the same in webserver it is encountering an error as
Server Error in '/' Application.
Divide by zero error encountered.
The statement has been terminated.
Using code 103 with CONVERT means that you're suppling as input a date in the format dd/mm/yy while in your code you are using mm/dd/yy format.
You could try with these values:
(1,'sa','ee','18/05/2013', 1)
For further reference about convert function check this out

DbCommand can't run multiple SQL command in one ExecuteNonQuery()

Hello I have the following problem and I can't solve it.
With DbCommand I'm trying execute this SQL statement
Dim strCommnad As String =
"CREATE DEFAULT [dbo].[DOMAIN_XLibPKID_D] AS (0);" + Environment.NewLine +
"CREATE TYPE [dbo].[XLibPKID] FROM BIGINT NOT NULL;" + Environment.NewLine +
"EXEC sp_bindefault 'DOMAIN_XLibPKID_D', 'XLibPKID';"
command.CommandText = strCommnad
command.CommandType = CommandType.Text
command.ExecuteNonQuery()
but I always get this an error message
Incorrect syntax near the keyword 'CREATE'.
But when I run each command from strCommand standalone then everything works fine.
I'm using VS 2010 Professional and SQL Server 2008 R2 Express.
Thanks for any help.
I'm not entirely sure what you're trying to do - but your approach seems overly complicated ...
You appear to be adding a DEFAULT clause to your XLibPKID column - right? This ALTER TABLE statement should do that, too:
command.CommandText =
"ALTER TABLE [dbo].[DOMAIN_XLibPKID_D] " +
" ADD CONSTRAINT DF_XLibPKID DEFAULT (0) FOR XLibPKID";
command.ExecuteNonQuery()
This just adds a separate DEFAULT CONSTRAINT to your table.
Try adding a GO between each statement:
Dim strCommnad As String = "CREATE DEFAULT [dbo].[DOMAIN_XLibPKID_D] AS (0);" + Environment.NewLine +
"GO; CREATE TYPE [dbo].[XLibPKID] FROM BIGINT NOT NULL;" + Environment.NewLine +
"GO; EXEC sp_bindefault 'DOMAIN_XLibPKID_D', 'XLibPKID';"

Resources