How to insert List <T> to local database? - windows-phone

I'm use local database in window phone .When I want insert data to table I'm use code as below:
ObjectTopicNews topic = new ObjectTopicNews
{
IDCat = IdCat,
Title=title,
Image=image
};
db.listData.InsertOnSubmit(topic);
db.SubmitChanges();
But I have a list data and I want insert all record of list to table and I don't know how to insert list to table,for example I want :
IList<ObjectTopicNews > list = getList();
db.listData.InsertOnSubmit(list );
db.SubmitChanges();

you will have iterate through list, retrieve object and insert
http://social.msdn.microsoft.com/Forums/vstudio/en-US/aaa25a0a-a7cb-4d44-9788-78f6d4b22355/how-to-iterate-through-list-or-ilist?forum=csharpgeneral

Related

How to retrive json data from sql table column into as a separate table

I have customer table with columns names
"orderid",
"body",
"time",
Below is the JSON data in side "body" column: {"PersonTitle":"Mr","FirstName":"david","LastName":"ford","PhoneNumber":""}.
How do i get the select data with column names orderid,firstname,phonenumber?
you should persist can come from an external source or generated through serialization from instances of objects.For sample;
foreach (var c in countries)
{
// Serialize the C# object to JSON
var json = JsonConvert.SerializeObject(c)
// Save content to the database
record.JsonColumn = json;
}
You can use Entity Framework (ORM), as well, to save JSON data into one column of a database table. For sample;
DECLARE #json NVARCHAR(MAX)
SET #json='{"PersonTitle":"Mr","FirstName":"david","LastName":"ford","PhoneNumber":"":["SQL","C#","MVC"]}';
SELECT *
FROM OPENJSON(#json);

How to compare 'OBJECT' stored as a BLOB inside the sqlite database?

I have a database which contains an OBJECT column. Can I compare the object stored inside that column with another object.? Can I read the individual datafields inside the object?
I have the following code which creates the table.
var sqlQuery:String = "create table test(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
+ "options OBJECT)";
var sqlStatement:SQLStatement = new SQLStatement();
sqlStatement.text = sqlQuery;
sqlStatement.execute();
To input the data, I do
var sqlQuery:String = "insert into test(options) values(#obj)";
var sqlStatement:SQLStatement = new SQLStatement();
sqlStatement.text = sqlQuery;
sqlStatement.parameters["#obj"] = myObject;
sqlStatement.execute();
SQLite has no OBJECT data type.
You can name the column type whatever you want, but as for the actual values, the documentation says:
Object column data is serialized in AMF3 format and stored using the BLOB storage class.
Such values can be compared with others, but this compares the entire value.
There are no built-in functions to extract fields from text or blob values, except for substr().

how to build loop to insert array in a table

i have the following code:
$var1="bmx"
$newdata = implode(",", $dx);
$queryinsert = "INSERT INTO table VALUES ($var1,$newdata)";
but with this code i get some like
INSERT INTO table VALUES (bmx,black,white, green, yellow,grey)";
and i need to create a new row for each color, like
INSERT INTO table VALUES (bmx,white)";
INSERT INTO table VALUES (bmx,black)";etc...
i think i should use a foreach but i don't know how.
You need something like this.
Go to the php docs, you will find everithing there.
$var1="bmx"
foreach ($dx as &$value) {
$queryinsert = "INSERT INTO table VALUES ($var1,&$value)";
}

Failing to insert binary stream into a BLOB in hsqldb?

I have the following hsqldb schema (as reported by SQLWorkbench):
DROP TABLE TEST CASCADE;
CREATE TABLE TEST
(
NAME VARCHAR(256),
METADATA VARCHAR(2048),
DATA BLOB
);
GRANT TRIGGER, INSERT, REFERENCES, DELETE, SELECT, UPDATE ON TEST TO DBA;
Next, I am trying to insert a file into the DATA field using the following prepared statement:
MERGE INTO test USING (VALUES ?, ?, ?) I (name, metadata, data) ON (test.name=I.name) WHEN MATCHED THEN UPDATE SET test.data = I.data, test.metadata = I.metadata WHEN NOT MATCHED THEN INSERT (name, metadata, data) VALUES (I.name, I.metadata, I.data)
Here is the code:
String name = ...;
String metadata = ...;
InputStream data = ...;
JDBCDataSource ds = new JDBCDataSource();
ds.setDatabase("jdbc:hsqldb:file:c:/tmp/file.db");
ds.setUser("sa");
ds.setPassword("");
PreparedStatement set = ds.getConnection().prepareStatement(m_setSql);
set.setString(1, name);
set.setString(2, metadata);
set.setBinaryStream(3, data);
set.executeUpdate();
The setBinaryStream fails, because the parameter type is deemed to be VARCHAR, rather than BLOB. Indeed, the function org.hsqldb.jdbc.JDBCPreparedStatement.setBinStream has the following statement:
if (parameterTypes[parameterIndex - 1].typeCode == Types.SQL_BLOB) {
setBlobParameter(parameterIndex, x, length);
return;
}
For the parameterIndex 3 it should enter the if-statement and invoke the setBlobParameter. But, for some reason, typeCode returns 12, which corresponds to VARCHAR, the if-statement is skipped and in the end an org.hsqldb.HsqlException is raised with the message of incompatible data type in conversion.
What am I doing wrong?
The types of the parameter values in the MERGE statement are unknown and default to VARCHAR. You need to cast the BLOB parameter to BLOB.
MERGE INTO test USING (VALUES ?, ?, CAST(? AS BLOB)) I (name, metadata, data)
ON (test.name=I.name)
WHEN MATCHED THEN UPDATE SET test.data = I.data, test.metadata = I.metadata
WHEN NOT MATCHED THEN INSERT (name, metadata, data) VALUES (I.name, I.metadata, I.data)

How to insert Byte[] into the Informix database through the SQL Editor interface

I wonder if there's some way to insert Byte[] in to my database column using the INSERT statement through my SQL Editor.
For example
INSERT INTO Temp (id,name) VALUES(1,'rg_book');
I just wanna to test my data and I don't want to make a user interface (file uploader ,...etc).
How to write this statement?
The CLR Byte array type (Byte[]) maps to a VARBINARY type in Informix DB2. See typing information here.
If your name field is expecting character data, use the VARBINARY function to convert the data into a binary representation of the string. See here.
For example:
INSERT INTO Temp (id, name) VALUES (1, VARBINARY('rg_book'));
If I were you I would do the following (if I've understood your question correctly):
Create test console project
Using Foreach or For (on your Byte[] array) compose required Insert's and (for example) add them to some file on a disk.
Run this script in Management Studio to fill in a table.
FileInfo f = new FileInfo(#"d:\Inserts.txt");
Byte[] list = {0, 1, 2};
using (StreamWriter w = f.CreateText())
{
for (int i = 0; i < list.Length; i++)
{
w.WriteLine("INSERT INTO [TEMP] ([id], [Name]) VALUES ({0}, 'rg_book')", list[i]);
}
}