I have extracted data from database into a datatable. And now i want to add data from this datatable into a resx file. I have created a resx file and datatable like this.
class LoadData
{
public static void main(string[] args)
{
string FilePath=@"C:\"
DataTable dt = new DataTable();
using(ResXResourceWriter rxrw = new ResXResourceWriter(FilePath+"Demo.resx"))
{
SqlConnection conn=new SqlConnection();
conn.ConnectionString="my connection string here";
SqlCommand cmd= new SqlCommand("my sql command here",conn);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
conn.Close;
da.Dispose();
}
}
}
So how can i load data from datatable dt into Demo.resx file?
You can do something like
public class Example
{
public static void Main()
{
// Instantiate an Automobile object.
Automobile car1 = new Automobile("Ford", "Model N", 1906, 0, 4);
Automobile car2 = new Automobile("Ford", "Model T", 1909, 2, 4);
// Define a resource file named CarResources.resx.
using (ResXResourceWriter resx = new ResXResourceWriter(@".\CarResources.resx"))
{
resx.AddResource("Title", "Classic American Cars");
resx.AddResource("HeaderString1", "Make");
resx.AddResource("HeaderString2", "Model");
resx.AddResource("HeaderString3", "Year");
resx.AddResource("HeaderString4", "Doors");
resx.AddResource("HeaderString5", "Cylinders");
resx.AddResource("Information", SystemIcons.Information);
resx.AddResource("EarlyAuto1", car1);
resx.AddResource("EarlyAuto2", car2);
}
}
Refer https://docs.microsoft.com/en-us/dotnet/framework/resources/working-with-resx-files-programmatically
This answer is not an answer to your question, but a possible solution to what I understood you are looking for (embedding a DataTable in an assembly).
The DataTable could be transformed to XML:
dt.WriteXml(@"c:\myDataTable.xml");
You drag&drop this xml file to your project at the desired location. (e.g. Resources directory)
Right-click on it and select "Embedded resource" as compile action.
Now it's available in your code with such a method:
public DataTable XmlToDataTable(string resourceName = "myDataTable.xml")
{
DataSet ds = new DataSet();
using (System.IO.Stream input = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MyProject.Resources." + resourceName))
{
try
{
ds.ReadXml(input);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}
return ds.Table[0];
}