How to Create new Database from Existing Data Base in c#? [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to create a new database using c#. I just want to pass database name from user interface and for that database name i want to run a sql script of database for creating the same schema of that script for new database.

I do not have exactly whay you intend to do, but I have done some functionality to seed some default data to the master tables.
//sql file location
private static readonly string IndexScriptSeedMasterDataLocation = "SqlSeedMasterData.sql";
In the function I have :
private static void SeedMasterData ( IpDataContext context, string databaseName)
{
context.Database.CreateIfNotExists();
var sqlContent = Content(IndexScriptSeedMasterDataLocation);
var modifiedSqlScript = sqlContent.Replace("#DatabaseName", databaseName);
context.Database.ExecuteSqlCommand(modifiedSqlScript);
}
// Content function :
private static string Content(string fileLocation)
{
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(fileLocation))
{
if (stream == null)
{
return string.Empty;
}
var streamReader = new StreamReader(stream);
return streamReader.ReadToEnd();
}
}

Related

Check if cyclic string or not

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 hours ago.
Improve this question
Enter a string of characters S. Indicates whether the string S is cyclic or not.
For example S='abcdabcdabcdabcd' is cyclic.
My teacher want me to do this but i think it's too hard. I'm thinking using SplitString but it's just an idea and i dont know how to do. I'm very thankful if someone gives me help
using System;
using System.Collections;
public class MYCLASSNAME {
public static void Main(string[] args){
var str = "abcdabcdabcdabcd";
Console.Write(checkCyclicString(str));
}
static bool checkCyclicString (string str){
var checkString ="";
for(var i=0;i<str.Length-1;i++){
checkString= checkString+ str[i];
var countOfCycles =0;
var haveCycles = false;
for(var j=i+1;j<str.Length-checkString.Length+1;j= j+checkString.Length){
if(checkString==str.Substring(j,checkString.Length)){
haveCycles = true;
countOfCycles++;
}else{
haveCycles =false;
}
}
if(haveCycles && countOfCycles == Math.Ceiling((double)(str.Length/checkString.Length))-1){
return true;
}
}
return false;
}
}
check the cyclic behavior by creating substrigs and comparing them with all the parts of the given string

how to get blocked file extensions and maximum file size for attachment in dynamics crm using c# code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to get blocked file extension and maximum file size for attachment set by admin in c# code .Below image displays what I actually want using c# code.
Please suggest me answer.
Please use the following code to get any property in the System Settings.
var query = new QueryExpression("organization")
{
ColumnSet = new ColumnSet("blockedattachments", "maxuploadfilesize")
};
EntityCollection orgCollection = _service.RetrieveMultiple(query);
if (orgCollection.Entities.Count > 0)
{
Entity org = orgCollection.Entities.First();
string blockedattachments = org.GetAttributeValue<string>("blockedattachments");
int numberMaxUploadFileSize = org.GetAttributeValue<int>("maxuploadfilesize");
}
Try using below code, it is tested and working fine.
var query = new QueryExpression("organization")
{
ColumnSet = new ColumnSet("blockedattachments", "maxuploadfilesize")
};
var record = service.RetrieveMultiple(query).Entities.FirstOrDefault();
if (record != null)
{
var blockedAttachments = record.GetAttributeValue<string>("blockedattachments");
var maxAttachmentSize = record.GetAttributeValue<int>("maxuploadfilesize");
}

Database creation only on first run of visual c# application [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Any one help me to create a database in MySQL only on the first run of my c# application. Second time when i run this application if database exists then creation of database code should be skipped.
If you use code first in the Entity Framework you can create your database from the classes in C# that define you tables and relationships.
Once you have your classes you just have to change or setup a connection string to your database and then run code... then the database will be created for you... by magic .. or your classes!!
There are many samples online
Code First with Azure
http://www.dotnetjalps.com/2015/04/entity-framework-code-first--mysql-azure.html
Entity Framework Code First - Create Database with MySql?
private static bool CheckDatabaseExists(SqlConnection tmpConn, string databaseName)
{
string sqlCreateDBQuery;
bool result = false;
try
{
tmpConn = new SqlConnection("server=(local)\\SQLEXPRESS;Trusted_Connection=yes");
sqlCreateDBQuery = string.Format("SELECT database_id FROM sys.databases WHERE Name
= '{0}'", databaseName);
using (tmpConn)
{
using (SqlCommand sqlCmd = new SqlCommand(sqlCreateDBQuery, tmpConn))
{
tmpConn.Open();
object resultObj = ExecuteScalar();
int databaseID = 0;
if (resultObj != null)
{
int.TryParse(resultObj.ToString(), out databaseID);
}
tmpConn.Close();
result = (databaseID > 0);
}
}
}
catch (Exception ex)
{
result = false;
}
return result;
}
This will work with any database name you pass in as a parameter, and it will return a bool true = database exists, false = database does not exist (or error happened).If true mean skip the creation false mean create database..

Get all lines from console within same application [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an application that writes all sorts of status update and times to the console. I also have an email function that emails to clients. I would like at the end of the application send an email with all lines from the console (same application).
There does not seem to be a function Console.ReadAllLines.
I saw some ideas with GetStdHandle but could get it to work.
Any ideas how I could do this in c# pls?
You can do this by implementing your own TextWriter and Console.SetOut
public class MyWriter : TextWriter
{
private List<string> lines = new List<string>();
private TextWriter original;
public MyWriter(TextWriter original)
{
this.original = original;
}
public override Encoding Encoding
{
get { return Encoding.Default; }
}
public override void WriteLine(string value)
{
lines.Add(value);
original.WriteLine(value);
}
//You need to override other methods also
public string[] GetLines()
{
return lines.ToArray();
}
}
And use it like this
var writer = new MyWriter(Console.Out);
Console.SetOut(writer);
Console.WriteLine("Hello world");
Console.WriteLine("Bye!");
var lines = writer.GetLines();
Reading information back that's already been output to the console is a backwards design. Instead, store the information away in a DB/File/Memory so it can be re-used. continue to display the output as you do. However, when you need to send an email dig the info out of the DB/File/Memory.
It could be done like:
List<string> outputList = new List<string>();
string output = GetOutput();//Run continuously...perhaps in a loop or event trigger..whatever applies
outputList.Add(output);
Console.Writeline(output);
//when ready
SendEmail(outputList);
You could write a wrapper class to take care of it easily.
public class ConsoleWriter()
{
public static List<string> AllLines = new List<string>();
public static WriteConsole(string text)
{
AllLines.Add(text);
Console.Write(text);
}
}
Then read AllLines when you want to send the mail.

How do I get the duration of a video file using C#? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Any library in C# that allows me to do that?
google result for http://johndyer.name/post/2005/07/22/Retreiving-the-duration-of-a-WMV-in-C.aspx
using WMPLib; // this file is called Interop.WMPLib.dll
WindowsMediaPlayerClass wmp = new WindowsMediaPlayerClass();
IWMPMedia mediaInfo = wmp.newMedia("myfile.wmv");
// write duration
Console.WriteLine("Duration = " + mediaInfo.duration);
// write named attributes
for (int i=0; i<mediaInfo.attributeCount; i++)
{
Console.WriteLine(mediaInfo.getAttributeName(i) + " = " + mediaInfo.getItemInfo(mediaInfo.getAttributeName(i)) );
}
You can try this Extension method.
using Shell32;
public static class Extension
{
public static string GetLength(this FileInfo info)
{
var shell = new ShellClass();
var folder = shell.NameSpace(info.DirectoryName);
var item = folder.ParseName(info.Name);
return folder.GetDetailsOf(item, 27);
}
}
I hope following code snippet will help you :
using WMPLib;
// ...your code here...
var player = new WindowsMediaPlayer();
var clip = player.newMedia(filePath);
Console.WriteLine(TimeSpan.FromSeconds(clip.duration));
and don't forget to add the reference of wmp.dll which will be
present in System32 folder.

Categories

Resources