简体   繁体   English

我不断收到错误 System.Data.SqlClient.SqlException: '#' 附近的语法不正确。

[英]I keep getting the error System.Data.SqlClient.SqlException: 'Incorrect syntax near '#'.'

Hi please help I keep getting the error System.Data.SqlClient.SqlException: 'Incorrect syntax near '#'.'嗨,请帮助我不断收到错误 System.Data.SqlClient.SqlException: '#' 附近的语法不正确。 below is the code Thanks下面是代码谢谢

private void AddBirthDayToCal(int startDayAtFlNumber)
        {
            var startDate = new DateTime(currentDate.Year, currentDate.Month, 1);
            var endDate = startDate.AddMonths(1).AddDays(-1);
            SqlConnection FamilyMembersConnection;
            FamilyMembersConnection = new SqlConnection("Data Source =.\\SQLEXPRESS; AttachDbFilename = " + Application.StartupPath + "\\MyFamily.mdf; Integrated Security = True; Connect                         Timeout = 30; User Instance = True");
            FamilyMembersConnection.Open();
            FamilyMembersCommand = new SqlCommand($"select * from FamilyMembers where Birthday between #{startDate.ToShortDateString()}# and #{endDate.ToShortDateString()}#", FamilyMembersConnection);
            FamilyMembersAdapter = new SqlDataAdapter();
            FamilyMembersAdapter.SelectCommand = FamilyMembersCommand;
            FamilyMembersTable = new DataTable();
            FamilyMembersAdapter.Fill(FamilyMembersTable);
            foreach (DataRow row in FamilyMembersTable.Rows)
            {
                var BirthDay = DateTime.Parse(Conversions.ToString(row["BirthDay"]));
                var link = new LinkLabel();
                link.Tag = row["FamilyMembersID"];
                link.Name = $"link{row["FamilyMembersID"]}";
                link.Text = Conversions.ToString(row["First_Name"]);
                listFlDay[BirthDay.Day - 1 + (startDayAtFlNumber - 1)].Controls.Add(link);
            }
        }

It's because you put '#' in the query这是因为您在查询中放入了“#”

FamilyMembersCommand = new SqlCommand($"select * from FamilyMembers where Birthday between #{startDate.ToShortDateString()}# and #{endDate.ToShortDateString()}#", FamilyMembersConnection);

Change it to be like this改成这样

FamilyMembersCommand = new SqlCommand($"select * from FamilyMembers where Birthday between '{startDate.ToShortDateString()}' and '{endDate.ToShortDateString()}'", FamilyMembersConnection);

The # in sql allows you to reference a temporary object or procedure within the current session. sql 中的# 允许您在当前会话中引用临时对象或过程。 That being said, you shouldn't need any # in the query.话虽如此,您不应该在查询中需要任何 #。 I'm assuming you'll want to replace those with single quotes as the string interpolation you are doing should resolve the properties you are passing in.我假设你想用单引号替换那些,因为你正在做的字符串插值应该解析你传入的属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不断收到 System.Data.SqlClient.SqlException:'';' 附近的语法不正确。' - Keep getting System.Data.SqlClient.SqlException: 'Incorrect syntax near ';'.' 收到错误“System.Data.SqlClient.SqlException:'')'附近的语法不正确。” - Getting an error "System.Data.SqlClient.SqlException: 'Incorrect syntax near ')'." 我在'nvarchar'错误消息附近获取了system.data.sqlclient.sqlexception不正确的语法 - I'm getting the system.data.sqlclient.sqlexception incorrect syntax near 'nvarchar' error message System.Data.SqlClient.SqlException:“)”附近的语法不正确 - System.Data.SqlClient.SqlException: Incorrect syntax near ')' System.Data.SqlClient.SqlException:关键字'file'附近的语法不正确 - System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'file' 修复异常 System.Data.SqlClient.SqlException: '''''附近的语法不正确。' - Fix the exception System.Data.SqlClient.SqlException: 'Incorrect syntax near '='.' System.Data.SqlClient.SqlException: ''=' 附近的语法不正确。 在数据表和对象上 - System.Data.SqlClient.SqlException: 'Incorrect syntax near '='.' on Datatable and object System.Data.SqlClient.SqlException:“ =”附近的语法不正确 - System.Data.SqlClient.SqlException: Incorrect syntax near '=' System.Data.SqlClient.SqlException:'值'附近的语法不正确。 - System.Data.SqlClient.SqlException: 'Incorrect syntax near 'value'.' System.Data.SqlClient.SqlException:','附近的语法不正确。 - System.Data.SqlClient.SqlException: 'Incorrect syntax near ','.'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM