我正在为我的新项目而努力,我在C#中是一个全新的人,所以我不太了解,我现在正在制作一个加载屏幕,您知道“加载消息”。 / p>
所以我想说几行,并且代码应该能够选择随机行并在加载时显示。像这个例子:
var randomLoadingMessage = function() {
var lines = new Array(
"Locating the required gigapixels to render...",
"Spinning up the hamster...",
"Shovelling coal into the server...",
"Programming the flux capacitor"
);
return lines[Math.round(Math.random()*(lines.length-1))];
}
我在互联网上找到了它。我如何在c#上使用它?
我需要帮助才能在c#中做类似的事情
答案 0 :(得分:1)
这是您需要解决的部分
//set initial catalog to destination database
string connStr = @"Data Source=YourServer;Initial Catalog=DestinationDatabase;Integrated Security=SSPI;";
using (SqlConnection conn = new SqlConnection(connStr))
{
//set source server and database using SMO objects
Server srv = new Server(@"YourServer");
srv.ConnectionContext.LoginSecure = true;
srv.ConnectionContext.StatementTimeout = 600;
Database db = srv.Databases["SourceDatabase"];
//configure Scripter for DDL
Scripter script = new Scripter(srv);
ScriptingOptions scriptOpt = new ScriptingOptions();
//SQL command to execute DDL in destination database
SqlCommand sql = new SqlCommand();
sql.Connection = conn;
conn.Open();
//this can changed to views, SPs, etc. as needed
foreach (Table t in db.Tables)
{
//check for system objects
if (!t.IsSystemObject)
{
StringCollection sc = t.Script(scriptOpt);
foreach (string s in sc)
{
//assign and execute DDL
sql.CommandText = s;
sql.ExecuteNonQuery();
}
}
}
}
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
}
}
string[] lines = new [] {
"Locating the required gigapixels to render...",
"Spinning up the hamster...",
"Shovelling coal into the server...",
"Programming the flux capacitor"
);
将为您提供字符串lines[1]
。您可以使用实例"Spinning up the hamster..."
类的Random
方法获得随机数。此方法要求您提供参数以定义结果应落入的范围。该方法返回一个Next
,但是您的索引需要一个double
,因此您必须转换结果。有关更多信息,请参见How do I generate a random int number in C#?。int
现在,我将其保留在此处,否则我将为您提供完整的解决方案。如果仍然有问题,请在此处发布新的问题,说明到目前为止的尝试以及为何不起作用(即,您是否收到错误消息,如果是,那是什么,或者它所做的事情与您期望的有所不同) )。