SQL Server 2008 password ending in a semicolon
Encapsulate your password in single quotes. e.g. given the password iloveachallenge;
your connection string should contain Password='iloveachallenge;';
.
I am using the following code to connect to SQL Server 2008 R2.
var sqlConnection = new SqlConnection()
{
ConnectionString = "Server=DT2719MOD;Database=abs2;User Id=TestUserLogon;Password='iloveachallenge;';"
};
sqlConnection.Open();
Console.WriteLine(sqlConnection.State);
sqlConnection.Close();
Edit:
Also tried to use the connection string that you have and it works on my machine.
ConnectionString="Data Source=DT2719MOD;Database=abs2;Trusted_Connection=no;User Id=TestUserLogon;Password='iloveachallenge;';"
How to have the semicolon character in a password for a SQL connection string?
Have you considered using SqlConnectionStringBuilder
?
var sqlBuilder = new SqlConnectionStringBuilder
{
DataSource = "localhost",
InitialCatalog = "mainDB",
Password = "Y;9r.5JQ6cwy@)V_",
UserID = "sqluser"
};
con = new SqlConnection(sqlBuilder.ToString());
How to handle MS SQL password with Semicolon and Single quotation in VB.Net Code?
You escape a double-quote with another double-quote, both in VB and in a connection string, and you can wrap connection string field values in double-quotes. That means that, to write your connection string literally, you would do this:
Dim ConnectionString As String = "Server=DT2719MOD;Database=abs2;User Id=TestUserLogon;Password=""ilo;veac'h""""alle;nge"""
The password gets wrapped in double-quotes in the connection string so that the semicolon is interpreted literally and they need to be escaped in VB. The double-quote in the password needs to be escaped in the connection string and then both of those need to be escaped in VB too.
Note that, as suggested in the comments, a connection string builder will do that for you, e.g.
Dim builder As New SqlConnectionStringBuilder With {
.DataSource = "DT2719MOD",
.InitialCatalog = "abs2",
.UserID = "TestUserLogon",
.Password = "ilo;veac'h""alle;nge"}
Dim connection As New SqlConnection(builder.ConnectionString)
In that case, the only thing you need to do is escape the double-quote in the password once for VB.
For the record, you could have used the Server Explorer to generate a connection string for you too.
Missing semicolon (;) at end of SQL statement.. Access Database With C#
You are adding the parentheses in the loop. That insert syntax is not even supported in MS SQL-Server < 2008, MS Access doesn't support it either. You are also adding the same parameters again and again, instead create the parameter once and overwrite the Value
in the loop. Also use the using
statement for everything that implements IDisposable
:
public void Main_InsertAllDatas(int currentUser, List<PasswordData> input)
{
string insert = "INSERT INTO tbl_Data ([UserName],[Password],[Category],[Title],[Description],[UserID]) VALUES (@UserName,@Password,@Category,@Title,@Description,@UserID)";
using (var MainConnection = new System.Data.OleDb.OleDbConnection("..."))
using (var Command = new System.Data.OleDb.OleDbCommand(insert, MainConnection))
{
Command.Parameters.Add(new System.Data.OleDb.OleDbParameter("@UserName", System.Data.OleDb.OleDbType.VarChar, 50));
Command.Parameters.Add(new System.Data.OleDb.OleDbParameter("@Password", System.Data.OleDb.OleDbType.VarChar, 50));
Command.Parameters.Add(new System.Data.OleDb.OleDbParameter("@Category", System.Data.OleDb.OleDbType.VarChar, 100));
Command.Parameters.Add(new System.Data.OleDb.OleDbParameter("@Title", System.Data.OleDb.OleDbType.VarChar, 100));
Command.Parameters.Add(new System.Data.OleDb.OleDbParameter("@Description", System.Data.OleDb.OleDbType.VarChar, 100));
Command.Parameters.Add(new System.Data.OleDb.OleDbParameter("@UserID", System.Data.OleDb.OleDbType.Integer));
MainConnection.Open();
for (int i = 0; i < input.Count; i++)
{
Command.Parameters["@UserName"].Value = EncDecWorker.EncrypteString(input[i].Username, Key);
// ...
try
{
MainConnection.Open();
int inserted = Command.ExecuteNonQuery();
} catch (Exception)
{
// log, show or do something else useful
throw; // better than an empty catch
}
}
} // you don't need to close the connection with using
}
Common Table Expression, why semicolon?
- To avoid ambiguity because WITH can be used elsewhere
..FROM..WITH (NOLOCK)..
RESTORE..WITH MOVE..
- It's optional to terminate statements with
;
in SQL Server
Put together, the previous statement must be terminated before a WITH/CTE. To avoid errors, most folk use ;WITH
because we don't know what is before the CTE
So
DECLARE @foo int;
WITH OrderedOrders AS
(
SELECT SalesOrderID, OrderDate,
...;
is the same as
DECLARE @foo int
;WITH OrderedOrders AS
(
SELECT SalesOrderID, OrderDate,
...;
The MERGE command has a similar requirement.
Error using semicolon in Azure SQL Server
As a workaround for this issue, please use Microsoft SQL Operations Studio as an alternative query editor for Linux and MAC. Download it from here. You won't have this issue with Operations Studio.
Users are currently demanding make SQL Server Management Studio a cross-platform tool as you can see here. For now you can use Microsoft SQL Operations Studio.
;' at the beginning of TSQL statements
It is supposed to be after the statements not before them. But in most cases in TSQL the terminating semi colons on statements are currently optional in practice (though technically Not ending Transact-SQL statements with a semicolon is deprecated) and the presence of statement terminating semi colons is not enforced.
An exception is the MERGE
statement (that does require a terminating semi colon) and also statements preceding WITH
or THROW
So this is a somewhat defensive practice for answers on StackOverflow in case the OP (or future readers) pastes it into the middle of some existing batch that doesn't have the required semi colon on the preceding statement and then complains it doesn't work and they receive the following error.
Incorrect syntax near the keyword 'with'. If this statement is a
common table expression, an xmlnamespaces clause or a change tracking
context clause, the previous statement must be terminated with a
semicolon.
In the case that the preceding statement is terminated with a semicolon the additional one does no harm. It just is treated as an empty statement.
This practice can itself cause problems though where the CTE is used in a context where multi statements are not valid. e.g. Inserting a semicolon before the WITH
here would break it.
CREATE VIEW V1
AS
WITH T(X)
AS (SELECT 1)
SELECT *
FROM T;
Similarly for THROW
blindly inserting a leading semi colon can cause problems too.
IF @i IS NULL
;THROW 50000, '@i IS NULL', 1;
Incorrect syntax near ';'.
I have fixed the example you give in your question and changed it to
;
--Ensure that any immediately preceding statement is terminated with a semicolon above
WITH cte
AS (SELECT ROW_NUMBER() OVER (PARTITION BY Col1, Col2, Col3
ORDER BY ( SELECT 0)) RN
FROM #MyTable)
DELETE FROM cte
WHERE RN > 1;
Related Topics
Join/Pivot Items with Eav Table
Why Isn't Postgres Using the Index
How to Select Records That Don't Exist in SQL Server
Comma-Separated String into Table's Column in SQL Server
Oracle as Keyword and Subqueries
Rolling Sum Previous 3 Months SQL Server
Varchar Requires a Length When Rendered on MySQL
SQL Server:Find Duplicates in a Table Based on Values in a Single Column
Oracle (11.2.0.1):How to Identify the Row Which Is Currently Updated by the Update Statement
SQL Server Normalization Tactic: Varchar VS Int Identity
Modify(Replace) Xml for Conditions
Choose As400 Query Records Directly from Excel
Query a Table and a Column Name Stored in a Table
How to Do Forward Fill as a Pl/Pgsql Function
Months Between Two Dates in SQL Server with Starting and End Date of Each of Them in SQL Server
Using Oracle Combine Three Tables to One with Pivot
Select Columns from One Table Based on the Column Names from Another Table