logo
down
shadow

WP Function to retrieve database connection


WP Function to retrieve database connection

By : user3865404
Date : January 10 2021, 01:57 PM
To fix this issue See the Interfacing with the Database docs.
You should just be able to use the global (curse you, Wordpress) variable $wpdb in your plugin functions, ie:
code :


Share : facebook icon twitter icon
How do I retrieve both the server name and database name from a connection string in web.config

How do I retrieve both the server name and database name from a connection string in web.config


By : Liching21
Date : March 29 2020, 07:55 AM
I wish this helpful for you How do I retrieve both the Server Name and Database Name from a web.config connection string programatically through the .net api? Preferably without using an html or xml parser I create. Looking for the simplest way to retrieve this type of information. Example web.config snippet: , Get the connectionstring from config as such:
code :
var myConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
var builder = new System.Data.OleDb.OleDbConnectionStringBuilder(myConnectionString);
var servername = builder["Data Source"];
var database = builder["Initial Catalog"];

Console.WriteLine("server={0}, database={1}", servername, database);
Is it possible to retrieve a MetadataWorkspace without having a connection to a database?

Is it possible to retrieve a MetadataWorkspace without having a connection to a database?


By : user3231472
Date : March 29 2020, 07:55 AM
seems to work fine Yes you can do this by feeding context a dummy connection string. Note that usually when you call parameterless constructor of DbContext, it will look for connection string with the name of your context class in app.config file of main application. If that is the case and you cannot change this behavior (like you don't own the source code of context in question) - you will have to update app.config with that dummy conneciton string (can be done in runtime too). If you can call DbContext constructor with connection string, then:
code :
var cs = String.Format("metadata=res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl;provider=System.Data.SqlClient;provider connection string=\"\"", "TestModel");
using (var ctx = new TestDBEntities(cs)) {
    var metadata = ((IObjectContextAdapter)ctx).ObjectContext.MetadataWorkspace;
    // no throw here
    Console.WriteLine(metadata);                
}
public static MetadataWorkspace GetMetadataWorkspaceOf<T>(string modelName) where T:DbContext {
    return new MetadataWorkspace(new[] { $"res://*/{modelName}.csdl", $"res://*/{modelName}.ssdl", $"res://*/{modelName}.msl" }, new[] {typeof(T).Assembly});
}
    public static MetadataWorkspace GetMetadataWorkspaceOfCodeFirst<T>() where T : DbContext {
        // require constructor which accepts connection string
        var constructor = typeof (T).GetConstructor(new[] {typeof (string)});
        if (constructor == null)
            throw new Exception("Constructor with one string argument is required.");
        // pass dummy connection string to it. You cannot pass empty one, so use some parameters there
        var ctx = (DbContext) constructor.Invoke(new object[] {"App=EntityFramework"});
        try {                
            var ms = new MemoryStream();
            var writer = new XmlTextWriter(ms, Encoding.UTF8);
            // here is first catch - generate edmx file yourself and save to xml document
            EdmxWriter.WriteEdmx(ctx, writer);
            ms.Seek(0, SeekOrigin.Begin);
            var rawEdmx = XDocument.Load(ms);
            // now we are crude-parsing edmx to get to the elements we need
            var runtime = rawEdmx.Root.Elements().First(c => c.Name.LocalName == "Runtime");                
            var cModel = runtime.Elements().First(c => c.Name.LocalName == "ConceptualModels").Elements().First();
            var sModel = runtime.Elements().First(c => c.Name.LocalName == "StorageModels").Elements().First();
            var mModel = runtime.Elements().First(c => c.Name.LocalName == "Mappings").Elements().First();

            // now we build a list of stuff needed for constructor of MetadataWorkspace
            var cItems = new EdmItemCollection(new[] {XmlReader.Create(new StringReader(cModel.ToString()))});
            var sItems = new StoreItemCollection(new[] {XmlReader.Create(new StringReader(sModel.ToString()))});
            var mItems = new StorageMappingItemCollection(cItems, sItems, new[] {XmlReader.Create(new StringReader(mModel.ToString()))});
            // and done
            return new MetadataWorkspace(() => cItems, () => sItems, () => mItems);
        }
        finally {
            ctx.Dispose();
        }
    }
Cannot retrieve database connection information from a separate file

Cannot retrieve database connection information from a separate file


By : Edithson Abelard
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further This has been asked numerous times and I've went through numerous solutions on stack overflow but no solution managed to fix my issue (even though it's so trivial). I've got a file called utilities.php which returns an array with the connection information and I call the array to a variable $cfg. I can echo the $cfg variable and it returns the correct information but when I'm trying to connect to the database and submit a query it fails to connect to the database. , you forget to pass connection param to mysqli_query function:
code :
    $link = mysqli_connect("localhost", "my_user", "my_password", "world");
    $query = "INSERT INTO comment (userID, commentText, adventureID, dateTime) VALUES('$userid', '$commentInput', '$adventureid', '$date')";
    mysqli_query ($link , $query );
Django app uses only default database to retrieve data. How to set the retrieve function to mysql databse

Django app uses only default database to retrieve data. How to set the retrieve function to mysql databse


By : Nilton Felipe de Oli
Date : March 29 2020, 07:55 AM
Hope that helps There are two database for my Django project. I had created function to enter data to the database (mysql), but the data used to always save in the default database. I have solved the problem now using .save(using='db2'). The current problem is that I have used instance to get the id of the user and when I want to see the user detail using the function below, the user id of the default database conflicts with the user id of the mysql database so the default databases detail is shown. How can I use only db2 like the one I used for saving data. The function for showing the userdetail is shown below. , Since you are using 'db2' change your instance to this:
code :
instance = get_object_or_404(Subscribers.objects.using('db2'), id=id)
Retrieve database server from connection URL

Retrieve database server from connection URL


By : VClyfe
Date : March 29 2020, 07:55 AM
this one helps. I am using ant script and I have following requirement. , Here is the regexp you're looking for:
code :
jdbc:sqlserver:\/\/(.+)?;
Related Posts Related Posts :
shadow
Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk