Instead, always use parameters for those values. Most importantly, one should never use string concatenation or interpolation to specify SQL statement values.
There are other serious issues with the code that also need to be addressed. So you simply need to remove that comma and add a close parenthesis for valid syntax. If you examine the query in a debugger, it will show a string like INSERT INTO IP (IP) VALUES (N'127.0.0.1', whereas it seems the intended query is INSERT INTO IP (IP) VALUES (N'127.0.0.1'). The reason for the syntax error is the extraneous trailing comma. The expectation pops up at : command.ExecuteNonQuery() *FileStream fs = new FileMode.Open, FileAccess.ReadWrite) Int rowsAffected = command.ExecuteNonQuery() //execute query and get updated row countĬonsole.WriteLine(rowsAffected + " row(s) updated") Using (SqlCommand command = new SqlCommand(sqlQuery, conn)) StrBuilder.Append($"UPDATE IP SET IP = N''") StrBuilder.Clear() // clear all the string Using (SqlCommand command = new SqlCommand(sqlQuery, conn)) //pass SQL query created above and connectionĬommand.ExecuteNonQuery() //execute the Query StrBuilder.Append("INSERT INTO IP (IP) VALUES ") StringBuilder strBuilder = new StringBuilder()
create a new SQL Query using StringBuilder SqlConnection conn = new SqlConnection(connString)
create instanace of database connection + database + " Persist Security Info=True User ID=" String connString = Source=" + datasource + " Initial Catalog=" If (IP.AddressFamily = AddressFamily.InterNetwork) Hostname = įoreach (IPAddress IP in Host.AddressList) String content = IPAddress = string.Empty I am trying to make a simple app that gets your IP address and stores it in a sql database 1 IF (1 = FULLTEXTSERVICEPROPERTY(''IsFullTextInstalled''))ĮXEC. ( NAME = N''Sk1_log'', FILENAME = N''C:\SQLDATA\Sk1_log.ldf'', SIZE = 3136KB, MAXSIZE = UNLIMITED, FILEGROWTH = 10%) ĪLTER DATABASE SET COMPATIBILITY_LEVEL = 100 ( NAME = N''Sk1'', FILENAME = N''C:\SQLDATA\Sk1.mdf'', SIZE = 12160KB, MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) IF EXISTS (SELECT name FROM sys.databases WHERE name = ''Sk1'') Quick suggestion, remove the SSMS specific batch delimiters "GO" and terminate each statement with a semicolon. = 'enable'ĪLTER DATABASE SET ANSI_NULL_DEFAULT OFFĪLTER DATABASE SET ANSI_PADDING OFFĪLTER DATABASE SET ANSI_WARNINGS OFFĪLTER DATABASE SET AUTO_CREATE_STATISTICS ON IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))ĮXEC. ( NAME = N'Sk1_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\Sk1_log.ldf', SIZE = 3136KB, MAXSIZE = UNLIMITED, FILEGROWTH = 10%)ĪLTER DATABASE SET COMPATIBILITY_LEVEL = 90 ( NAME = N'Sk1', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\Sk1.mdf', SIZE = 12160KB, MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) IF EXISTS (SELECT name FROM sys.databases WHERE name = 'Sk1') This script works in SSMS, but when compiled using SQL Packager, it errors out. Can someone look over this script and tell me why I'm getting an "Incorrect syntax near 'Go'" error right at the line above the dashes