nHibernate select query? - vb.net

I have an ado.net statement I want to convert to use NHibernate:
Dim sql As New StringBuilder()
sql.AppendLine("SELECT r.RoleId, r.RoleName ")
sql.AppendLine("FROM dbo.aspnet_Roles r ")
sql.AppendLine("WHERE r.RoleId IN ")
sql.AppendLine(" (select roleID from dbo.MenuRole where menuId = #MenuId) ")
sql.AppendLine("Order By r.RoleName")
later, I populate the parameter with:
cmd.Parameters.AddWithValue("#MenuId", menuId)
Considering I want to return an: IList(Of AspnetRole)
and I'm using:
Dim managerFactory As IManagerFactory = New ManagerFactory()
Dim roleManager As IAspnetRoleManager = managerFactory.GetAspnetRoleManager()
How do I build and use that query with nHiberate?
(P.S. I am using Codesmithtools and VB.net and VS2008 and SQL Server 2008)

First, you'd have to map the aspnet_roles table and the MenuRole table to their corresponding classes. Once you've mapped them, I'd also map a many-to-one MenuRole property to the AspnetRole class.
Once you've done that the criteria query should look something like this:
Dim c As ICriteria = Session.CreateCriteria(TypeOf(AspnetRole))
c.Add(Restrictions.Eq("Menu.Id", menuId)
return c.List(Of AspnetRole)

Related

How to use parameters in Visual Studio 2022 with an Access Database

I have created a database in Access 2019
I have created a basic form to display the data from the above table
I would like to filter the data to show only certain countries – like below
The where clause is hard code and so my question is how can I dynamically change the filter clause say from ‘Aus’ to ‘UK’.
a) I have tried using a parameter ‘CountryName’ as see in the Fill, GetData (CountryName), but I am unable to use the parameter in the Query Builder. How can this be done if possible?
b) Is there a way to change the Fill Query Property (CommandText) by code as I am unable to see the correct properties to use – see below
It sounds like you are creating a typed DataSet. In that case, just leave the default query as it is for each table adapter. You can then call Fill or GetData on a table adapter to get all the data in the corresponding table. If you want to be able to filter the data, add a new query with method names that reflect the filter, e.g. if you want to filter by the CountryName column then name the methods FillByCountryName and GetDataByCountryName. In the Query Builder, you have to use ? as parameter placeholders rather than names like #CountryName, e.g.
SELECT * FROM MyTable WHERE CountryName = ?
In code, you would then do something like this:
Dim countryName = "UK"
myTableAdapter.FillByCountryName(myDataSet.MyTable, countryName)
You can try this:
DECLARE #CountryName AS NVARCHAR(50) = 'uk'
SELECT CountryName FROM MyTable
WHERE CountryName = #CountryName
How this would translate to code:
Public Sub GetCountry()
DefaultCatalog = "MyTable"
Dim selectStatement = "SELECT CountyrName FROM MyTable WHERE CountyrName = #CountryName"
Using cn As New SqlConnection With {.ConnectionString = ConnectionString}
Using cmd As New SqlCommand With {.Connection = cn, .CommandText = selectStatement}
cmd.Parameters.AddWithValue("#CountryName", "uk")
cn.Open()
Dim reader = cmd.ExecuteReader()
If reader.HasRows Then
reader.Read()
Console.WriteLine(reader.GetString(0))
End If
End Using
End Using
End Sub

Delete all position in DbContext from BindingSource.List using Entity (EF6)

I have DbContext with table named:base.books
and BindingSource from LINQ query with records which I want to delete using RemoveRange.
Dim query = From books
In base.books
Where //some conditions
Select books.id, books.name,
Dim BooksSource As New BindingSource
BooksSource.DataSource = query.ToList
DataGridView_Books.DataSource = BooksSource.DataSource
After some changes in DataGridView_Books I want delete all positions from DataGridView_Books in base.books.
I imagine it that way:
listA=BooksSource.DataSource.List
''listB=convert(listA)
base.books.RemoveRange(listB)
base.books.SaveChanges()
but I do not know how to replace convert (I mean: 'System.Collections.IList' to 'System.Collections.IEnumerable')
Thanks.

String.Format vs Parameter Values SQL Query

I'm trying to figure out if there is a better way to do this
Dim cmd As New SqlCommand
Dim sel As String
Dim obj As New DataHandler
sel = String.Format("SELECT * FROM Customers WHERE Country LIKE '{0}%'", txt_Input.Text)
cmd.CommandText = sel
Me.dgv_Customers.DataSource = obj.SqlDataRetriever(cmd)
Basically what im trying to do is have a textbox that whenever I type a letter, the grid refreshes itself by sending a Query to my SQL server searching for whatever its in the textbox using the LIKE() from SQL. I've been reading about SQL injection and so far everyone suggests to use parameter values (#value) for user input, but if I try to replace the {0} with that it doesn't work. I just wanna make sure that this is a valid way of doing this.
Thanks
Instead just concatenate the string like below. You should consider using parameterized query to avoid SQL Injection.
sel = "SELECT * FROM Customers WHERE Country LIKE '" + txt_Input.Text + "%'";
Use a parameterized query rather. See This Post
Dim cmd as New SqlCommand("SELECT * FROM Customers WHERE Country LIKE #param")
cmd.Parameters.Add("#param", txt_Input.Text +"%")

How to get column names for a linq sql query result in vb 2010

I've seen C# examples but can not find a way to get the column names from the results of a LINQ SQL query in VISUAL BASIC.
So myLINQ query looks like this:
Dim dbs As New DataClasses1DataContext
Dim s As Table(Of sched) = dbs.GetTable(Of sched)()
Dim sQuery = From sp In s
Where sp.custID = cID
Select sp Order By sp.scheddate
There are 13 fields in the table sched, and I'd like to be able to read off the column names in a foreach type loop. Can anyone help me?

VB.NET Multiple Selects at once using SQL Server CE

I have an array list which contains ids for some items. I would like to perform a multiple select at once from a SQL Server CE database and using my array list which contains what items id to be selected, something similar when doing for example multiple update in oracle (ODP.NET) as explained here: Oracle bulk updates using ODP.NET
where you can pass an array as a parameter.
I would like to do the same but for a multiple select instead in case of SQL Server CE. Is it possible?
DRAFT about what I would like to do:
SqlCeCommand = SqlCeConnection.CreateCommand()
SqlCeCommand.CommandText = "SELECT * FROM MyTable WHERE Id=:ids"
SqlCeCommand.CommandType = CommandType.Text
SqlCeCommand.Parameters.Add(":ids", DbType.Int32, ArrayListOfIds, ParameterDirection.Input)
Using reader As System.Data.SqlServerCe.SqlCeDataReader = SqlCeCommand.ExecuteReader()
Using targetDb As Oracle.DataAccess.Client.OracleBulkCopy = New Oracle.DataAccess.Client.OracleBulkCopy(con.ConnectionString)
targetDb.DestinationTableName = "MyTable"
targetDb.BatchSize = 100
targetDb.NotifyAfter = 100
targetDb.BulkCopyOptions = Oracle.DataAccess.Client.OracleBulkCopyOptions.UseInternalTransaction
AddHandler targetDb.OracleRowsCopied, AddressOf OnOracleRowsCopied targetDb.WriteToServer(reader)
targetDb.Close()
End Using
reader.Close()
End Using
You should try this approach by constructing your "IN" clause and adding each parameter in a for each loop:
SqlCeCommand = SqlCeConnection.CreateCommand()
SqlCeCommand.CommandType = CommandType.Text
Dim sb As New StringBuilder()
Dim i As Integer = 1
For Each id As Integer In ArrayListOfIds
' IN clause
sb.Append("#Id" & i.ToString() & ",")
' parameter
SqlCeCommand.Parameters.Add("#Id" & i.ToString(), DbType.Int32, id, ParameterDirection.Input)
i += 1
Next
If you're calling a Stored Procedure, you can do this:
Serialize the array to a string of XML, like this: https://stackoverflow.com/a/6937351/734914
Call the stored procedure, passing in the string parameter
Parse the string of XML into a local table variable containing the ID's, like this: https://stackoverflow.com/a/8046830/734914
Execute whatever queries you need to using the ID's
The links that I referenced might not be the best examples on the web, but the concept of "serialize to XML, pass string parameter, deserialize XML" should work here