在下面的下面的例子中,我返回从一个数据库中的值,并且由于是NULL值转换为双,如果它翻倒,则0被设置为默认值.
using (SqlCommand cmd = new SqlCommand(sql.ToString(), conn))
{
try
{
this.value = Convert.ToDouble(cmd.ExecuteScalar());
}
catch (Exception)
{
this.value = 0;
}
}
Run Code Online (Sandbox Code Playgroud)
通过在这种情况下使用Try Catch,这会被视为不良做法吗?什么是处理这种情况的更好方法?
我正在使用 Visual Studio 2010。我对代码做了一些更改,但后来想在编辑之前查看代码块的外观。所以我做了Ctrl+ Zundo 然后继续使用Ctrl+复制预期的代码块C。现在当我做Ctrl+ 时Y,重做没有完成,我的所有更改都丢失了。
请帮帮我。
我觉得我在这个问题上退了两步,但有些事情让我感到困惑.我正在通过TCP/IP和byte []进行一些通信.
所以我开始构建我的byte []数组,第三个字节需要byte []的长度.如果我这样声明我的字节;
byte[] bytesToSend = new byte[119];
Run Code Online (Sandbox Code Playgroud)
然后用数据填充前三个字节..
bytesToSend[0] = 0x40;
bytesToSend[1] = 0x40;
bytesToSend[2] = Encoding.ASCII.GetBytes(bytesToSend.Length.ToString())[0];
Run Code Online (Sandbox Code Playgroud)
最后只打印出第三个字节,其中应包含长度;
MessageBox.Show(BitConverter.ToString(bytesToSend));
Run Code Online (Sandbox Code Playgroud)
我应该期望它返回119的字节大小,或者这只是最大大小?目前它正在返回十六进制"0x31",据我所知,它不等于119或3.毫无疑问,我缺少一些简单/基本的东西,但是有人能指出我正确的方向吗?
我正在做简单的事情,这是有道理的,
IF EXISTS(SELECT * FROM sysobjects WHERE xtype = 'TF' AND name = 'GetGeographyFromSourceID')
BEGIN
DROP FUNCTION dbo.GetGeographyFromSourceID
END
GO
CREATE FUNCTION dbo.GetGeographyFromSourceID
(
@SourceID INT
)
RETURNS GEOGRAPHY
AS
BEGIN
DECLARE @Longitude FLOAT = 0;
DECLARE @Latitude FLOAT = 0;
DECLARE @Geo GEOGRAPHY;
SELECT @Longitude = Longitude,
@Latitude = Latitude
FROM Sources
WHERE ID = @SourceID;
SELECT @Geo = geography::STGeomFromText('POINT(' + CONVERT(VARCHAR( 100), @Longitude) + ' ' + CONVERT( VARCHAR( 100),@Latitude) + ')', 4326);
RETURN(@Geo)
END
Run Code Online (Sandbox Code Playgroud)
如果存在而不是删除它并重新创建它.但我收到了错误There is …
我试过写服务器端应用程序.从学校开始,我倾向于不要让代码无限循环.但是,在我的服务器端应用程序中,我还没有找到在什么条件下我应该从无限循环中退出的方式.代码
ServerSocket s = null;
s = new ServerSocket(5000 , 10);
Socket connection = null;
while(true)
{
connection = s.accept();
//create new thread to handle client
new client_handler(connection).start();
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用从dbcontext继承的类来运行不带参数的存储过程。这是我的代码:
MyDBContext _myDBContext = new MyDBContext();
_myDBContext.Database.SqlQuery("MyStoredProcedure", new SqlParameter());
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为它正在寻找错误的过载。即使我使用的是字符串,也可以使用object []。
如果我在那里有参数,它就可以工作。我已经用其他存储过程做到了这一点,例如:
IEnumerable<MyClass> tempMyClass =
_myDbContext.Database.SqlQuery<MyClass>(
"myStoredProcedure @ID",
new SqlParameter("@ID", 5));
Run Code Online (Sandbox Code Playgroud)
知道我要去哪里错了吗?
class MontagueDBContext : DbContext, IUnitOfWork
{
public MontagueDBContext() : base("name=MontigueApp")
{
AppDomain.CurrentDomain.SetData("DataDirectory", System.IO.Directory.GetCurrentDirectory());
}
public IDbSet<Permit> Permits { get; set; }
public void ChangeEntityState<TEntity>(TEntity item, EntityState entityState) where TEntity : class
{
this.Entry(item).State = entityState;
}
public EntityState GetEntityState<TEntity>(TEntity item) where TEntity : class
{
return this.Entry(item).State;
}
public new int SaveChanges()
{
return base.SaveChanges();
}
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码,我得到一个无法访问的语句错误被抛出.有关该行说它是罪魁祸首之后有一条评论.
public static void selectPlayer ()
{
// Loops through Player class' static array. If an index is storing a player object then print the player name out with a number for selection.
for(int i=0; 1<101; i++)
{
if (Player.playerArray[i] != null)
{
System.out.println(i + ". " + Player.playerArray[i - 1].playerName);
}
System.out.print("\nPlease enter the number that corresponds to the player you wish to use; "); // This line is where it failed to compile a la unreachable statement.
Scanner …Run Code Online (Sandbox Code Playgroud)