Sql excecute标量在不调试时返回null [英] Sql excecute scalar returning null when not debugging
问题描述
你好
我正在编写数据库迁移器并执行以下函数。
如果在第一个函数中创建数据库表,则第二个函数总是返回null然后在trim函数调用时崩溃。问题是,如果我在执行标量函数上放置一个断点,它可以正常工作并返回db_version上的值,但是如果我将断点稍微低于版本值则为null
任何想法?< br $>
private static void CreateVersionTable()
{
if(!DatabaseManager.TableExists) (poscuba_info))
{
string sqlTable = @CREATE TABLE poscuba_info(
db_version CHAR(10));;
string sqlVersion = @INSERT INTO poscuba_info(db_version)VALUES('1.02');
尝试
{
DatabaseManager.executeSQL(sqlTable);
DatabaseManager.executeSQL(sqlVersion);
dataBaseVersion =1.02;
}
catch(例外e)
{
MigrationError = e.Message;
}
}
}
公共静态字符串GetDBVersion()
{
string version =;
try
{
version =(string)DatabaseManager.executeScalar(从poscuba_info中选择db_version);
if(version ==)
{
MigrationError =找不到版本;
}
}
catch(例外e)
{
MigrationError = e.Message;
}
dataBaseVersion = version.Trim();
返回dataBaseVersion;
}
我尝试过:
我已经尝试过调试,它在逐步调试时有效
这可能是发布模式下的计时问题,请尝试添加以下内容:
DatabaseManager.executeSQL(sqlTable);
Thread.Sleep( 1000 ); // 在这里添加1s等待
DatabaseManager.executeSQL(sqlVersion);
hello
I am coding a database migrator and I execute the following functions.
The second one if the database table was created in the first function always return null and then crashes on the trim function call. The thing is that if I place a breakpoint on the execute scalar function it works fine and returns the value on the db_version but if I place the breakpoint a little below the version value is null
Any idea?
private static void CreateVersionTable() { if (!DatabaseManager.TableExists("poscuba_info")) { string sqlTable = @"CREATE TABLE poscuba_info( db_version CHAR(10));"; string sqlVersion = @"INSERT INTO poscuba_info (db_version) VALUES ('1.02') "; try { DatabaseManager.executeSQL(sqlTable); DatabaseManager.executeSQL(sqlVersion); dataBaseVersion = "1.02"; } catch (Exception e) { MigrationError = e.Message; } } } public static string GetDBVersion() { string version = ""; try { version = (string)DatabaseManager.executeScalar("Select db_version from poscuba_info"); if (version == "") { MigrationError = "Version not found"; } } catch (Exception e) { MigrationError = e.Message; } dataBaseVersion = version.Trim(); return dataBaseVersion; }
What I have tried:
I have tried debugging and it works when debugged step by step
It is possibly a timing issue in release mode, try adding the following:
DatabaseManager.executeSQL(sqlTable); Thread.Sleep(1000); // add a 1s wait here DatabaseManager.executeSQL(sqlVersion);
这篇关于Sql excecute标量在不调试时返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!